Is it correctly understood that the following two functions do the exact same? No matter how they are invoked.
def test():
file = open("testfile.txt", "w")
file.write("Hello World")
def test_2():
with open("testfile.txt", "w") as f:
f.write("Hello World")
Since python invokes the close method when an object is no longer referenced.
If not then this quote confuses me:
Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.
No, the close
method would be invoked by python garbage collector (finalizer) machinery in the first case, and immediately in the second case. If you loop calling your test
or test_2
functions thousands of times, the observed behavior could be different.
File descriptors are (at least on Linux) a precious and scarce resource (when it is exhausted, the open(2) syscall fails). On Linux use getrlimit(2) with RLIMIT_NOFILE
to query the limit on the number of file descriptors for your process . You should prefer the close(2) syscall to be invoked quickly once a file handle is useless.
Your question is implementation specific, operating system specific, and computer specific. You may want to understand more about operating systems by reading Operating Systems: Three Easy Pieces .
On Linux, try also the cat /proc/$$/limits
or cat /proc/self/limits
command in a terminal. You would see a line starting with Max open files
(on my Debian desktop computer, right now in december 2019, the soft limit is 1024). See proc(5) .
No. The first one will not save the information correctly. You need to use file.close()
to ensure that file is closed properly and data is saved.
On the other hand, with
statement handles file operations for you. It will keep the file open for as long as the program keeps executing at the same indent level and as soon as it goes to a level higher will automatically close and save the file.
More information here .
In case of test
function, close
method is not called until Python garbage collector will del f
, in this case it's invoked by file __del__
magic method which is invoked on variable deletion.
In case of test_2
function, close
method is called when code execution goes outside of with
statement. Read more about python context managers which is used by with
statement.
with foo as f:
do_something()
roughly is just syntax sugar for:
f = foo.__enter__()
do_something()
f.__exit__()
and in case of file, __exit__
implicitly calls close
No, it is not correctly understood. The close
method is invoked via the __exit__
method, which is only invoked when exiting a with
statement not when exiting a function. Se code example below:
class Temp:
def __exit__(self, exc_type, exc_value, tb):
print('exited')
def __enter__(self):
pass
def make_temp():
temp = Temp()
make_temp()
print('temp_make')
with Temp() as temp:
pass
print('temp_with')
Witch outputs:
temp_make
exited
temp_with
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.