0

I need to write a program that will write and read to/from a file. I have code that works depending on the order I call functions.

def FileSetup():
 TextWrite = open('Leaderboard.txt','w')
 TextWrite.write('''| Driver | Car | Team | Grid | Fastest Lap | Race Time | Points |
''')
 TextWrite.close()
 TextRead = open('Leaderboard.txt','r')
 return TextRead
def SortLeaderboard(LeaderBoard):
 TextFile = open('Leaderboard.txt', 'w')
 for items in LeaderBoard:
 TextFile.write('\n| '+items['Driver']+' | '+str(items['Car'])+' | '+items['Team']+' | '+str(items['Grid'])+' | '+items['Fastest Lap']+' | '+items['Race Time']+' | '+str(items['Points'])+' |')
Leaderboard = Setup()
FileSetup()
TextRead = FileSetup()
TextFile = open('Leaderboard.txt','w')
SortLeaderboard(Leaderboard)
#TextRead = FileSetup()
str = TextRead.read()
print str

Depending on which TextRead = FileSetup() I comment out either SortLeaderboard or FileSetup will work. If I comment out the TextRead after I call SortLeaderboard then SortLeaderboard will write to the file and FileSetup won't. If I call it after then FileSetup will write to the file and Sortleaderboard won't. The problem is only one function writes to the file. I am not able to get both to write to it.

I'm sorry this is really confusing this was the best way I could think of explaining it. If you need me to explain something in a different way just ask and I will try,

asked Nov 9, 2015 at 15:02
6
  • 2
    So... what exactly are you trying to accomplish? It looks like you have no problem reading to and from a file. It would be helpful if you clearly defined the problem, then showed what you've tried to accomplish it. Commented Nov 9, 2015 at 15:05
  • Added what the problem is. If the SortLeaderboard function writes to it then there will be nothing from the FileSetup function. And vice versa. Commented Nov 9, 2015 at 15:09
  • 1
    Where's the code for Setup function? You're calling FileSetup() twice and leaving the file open in 'r' mode, and you're attempting opening that file in like 4 different places... Commented Nov 9, 2015 at 15:09
  • Are you trying to append to existing file? Open it with open(filename, 'a') Commented Nov 9, 2015 at 15:10
  • You really want to separate the code that sorts the data from the code that handles the file input. The other thing is that you would be better suited using the file opening context managers because that way you won't run into the problem of opening files without closing them like you have here. Commented Nov 9, 2015 at 15:11

1 Answer 1

1

Avoid calling .open and .close directly and use context managers instead. They will handle closing the file object after you are done.

from contextlib import contextmanager
@contextmanager
def setup_file():
 with open('Leaderboard.txt','w') as writefile:
 myfile.write('''| Driver | Car | Team | Grid | Fastest Lap | Race Time | Points |
''')
 with open('Leaderboard.txt','r') as myread:
 yield myread
def SortLeaderboard(LeaderBoard):
 with open('Leaderboard.txt', 'w') as myfile:
 for items in LeaderBoard:
 TextFile.write('\n| '+items['Driver']+' | '+str(items['Car'])+' | '+items['Team']+' | '+str(items['Grid'])+' | '+items['Fastest Lap']+' | '+items['Race Time']+' | '+str(items['Points'])+' |')
Leaderboard = Setup()
with setup_file() as TextRead:
 SortLeaderboard(Leaderboard)
 str = TextRead.read()
print str

Here you instantiate your own context manager setup_file that encapsulates preparing the file for use, and cleaning up afterwards.

A context manager is a python generator with a yield statement. The control of flow is passed from the generator to the body of the generator after the yield statement.

After the body of the generator has been executed, flow of control is passed back into the generator and cleanup work can be done.

open can function as a context manager by default, and takes care of closing the file object.

answered Nov 9, 2015 at 15:33
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.