Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why isn't my script printing all my results to the file?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading