1
fileN = open("test1.txt", 'w')
print('255 255 255', file = fileN)
#fileN.close()

The file "test1.txt" would be empty without the final closing statement.

def foo():
    fileN = open("test2.txt", 'w')
    print('255 255 255', file = fileN)
    #fileN.close()

foo()

However, if I wrote the code in a function and then call it, the file would be written successfully even without the closing the statement.

So I have 2 questions: Does a file have to be closed to be written? Will python close the file automatically when the function is over?

  • 1
    "Python" won't. However, CPython the implementations uses reference counting, and your file *will* be reclaimed by this automatic memory management, and in which case, `file.__del__` will be called, which calls `file.close`. However, you shouldn't rely on this. The idiomatic way to work with files is to use a context manager, a `with` statement, and if not, you should definitely close your files explicitly. – juanpa.arrivillaga Nov 18 '19 at 19:16
  • 1
    Does this answer your question? [How come a file doesn't get written until I stop the program?](https://stackoverflow.com/questions/9824806/how-come-a-file-doesnt-get-written-until-i-stop-the-program) – Endzeit Nov 18 '19 at 19:17
  • 1
    The [File IO portion](https://docs.python.org/3.8/tutorial/inputoutput.html#reading-and-writing-files) of the Python tutorial will also answer these questions for you. – b_c Nov 18 '19 at 19:17
  • I think best practice is use `with open()` [with open](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) to write to the file. btw if you use `open()` you need to close the file. [why-should-i-close-files](https://stackoverflow.com/questions/25070854/why-should-i-close-files-in-python). If you don't close them yourself, Python will eventually close them for you. – Sha Nov 18 '19 at 19:22

2 Answers2

1

If you can't remember to close the file after writing to it, use this instead.

with open(filename):

When you use with open(), Python will automatically close the file for you at the end of execution.

Python will close the file even if you do not invoke file.close(), but the time it takes is unpredictable, it can slow your program down. And the edit you do to the opened file will not be applied until the file is closed by python eventually.

If you want the data to appear in the actual file immediately after the write(), you can do a file.flush(). Otherwise, it will automatically do for you during file.close().

S.B
  • 13,077
  • 10
  • 22
  • 49
HelloWorld
  • 290
  • 3
  • 14
  • Thanks for your explanation! But I still wonder why the file is written successfully when the code is put into a function. – Emelda Showmen Nov 18 '19 at 20:54
  • @ambidextrous From my understanding, it does eventually gets written, just you won't know when. It can be seconds, minutes, even hours. – HelloWorld Nov 18 '19 at 21:30
0

Usually, the file closes when the function is finished. There are some cases where this isn't true, but otherwise you don't need to worry about it too much.

Frasher Gray
  • 222
  • 2
  • 14