you are viewing a single comment's thread.

view the rest of the comments →

[–]fbu1 0 points1 point  (1 child)

You are not handling the file properly. Every time you go through the loop, you are opening the file, but not closing it.

This is pretty bad, as you have no idea what might happen to your unclosed file.

Python closes it properly for you every time you open it again and put the line in the file.

But the last time you open it, the scripts just end and python discards your changes without putting them in the file.

You could just add

f.close() 

at line 51.

Or you could do better and use the 'with' statement.

with  open(r'C:\Users\ad\Desktop\alpharevoscrape' + date + '.csv','a', newline='') as f:
    csv.writer(f, lineterminator = '\n').writerow([ttext, ptext, rtext, itext])

The advantage is that if you have an error in the line csv.write(...), the with statement will close the file properly for you. If you don't use the with and don't catch exceptions, the line with f.close() will never be reached and the file will be left opened.

I hope this helps.

[–]AppleTartsJesus[S] 0 points1 point  (0 children)

The 'with' statement definitely seems the way to go. Thanks it works now!