Issue
I have the simple code below that loops through a dataframe and prints the results to the screen and also to a file.
My nag issue is however, it prints all the data to the screen just perfectly, but the file is only getting the last end of the data.
Here is my code:
for star in Constellation_data(starDf.values.tolist()):
print(star)
sourceFile = open('stars.txt', 'w')
print(star, file = sourceFile)
sourceFile.close()
I open the file, then print to it, then close. So I not sure why it doesn't contain all the data like the screen has.
Thanks!
Solution
"w" deletes the existing file so for each iteration of the loop, you delete any previous content written. The normal way to handle this issue is to open the file once before the loop
with open('stars.txt', 'w') as sourceFile:
for star in Constellation_data(starDf.values.tolist()):
print(star)
print(star, file = sourceFile)
Note the with
clause - it will automatically close the file when done.
If there is a reason why you want to close the file on each write (perhaps another file is reading it or you want to save state more often), then you can use append mode. I've added code to delete the old file and then append on each loop. The first append will create the file.
if os.path.exists('stars.txt'):
os.remove('stars.txt')
for star in Constellation_data(starDf.values.tolist()):
with open('stars.txt', 'a') as sourceFile:
print(star)
print(star, file = sourceFile)
Answered By - tdelaney
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.