I believe what you are looking for is this:
with open(FileName , mode = APPEND) as f:
for U in List :
print(U)
f.write(U)
f.write("\n")
print("File written successfully.")
using with
will allow you to open the file, and python with automatically close it for you should an exception occur while it’s in use. You want to open the file before you enter your loop, then append within the loop, and finally, print your success message after closing the file (outside the with
). Indentation is important! 🙂
4
solved The Python file problems