Question

I use this since 12 years:

content=open(foo).read()

New colleagues coming from university say: You should use the with statement.

with open(foo) as fd:
    content=fd.read()

I don't see a good reason why I should type more than necessary.

The only benefit the with statement gives me: The fd gets closed as soon as the block gets left. Without the with statement the fd gets closed if the garbage collector starts to do his job.

Please tell me: Why should I use the with statement if I read a whole file at once?

Update: I know how the with statement works, I know that it is useful (e.g. writing to a file).

Was it helpful?

Solution 3

That you read the whole file at once has no impact on when the file is closed. No matter what you do with the file (short of explicitly closing it), without with the file remains open until its __del__ method is called. So the reason for using with apply just as much to your use case as to every other case of file I/O:

It definitely closes the file as early as possible. It's immune to reference cycles and it's more portable to other Python implementations (imagine the anger when you could get a 5x performance increase from PyPy but can't use it because your program crashes from having too many files open!). Even if it doesn't matter for the current context, code gets moved around and requirements change. It's an easy way (just one extra line!) to future proof code.

If it's a one-off script that will only open two files per run, it's still good style and a good habit. Same reason you don't change indentation to five spaces in one-off scripts.

OTHER TIPS

Well for your purposes, there is no real reason. However, withs usefulness is not limited to files alone. It can be used with threads. For example:

import threading
lock = threading.Lock()
with lock:
    # Critical section
    statements
    # End critical section

The with statement automatically acquires and releases a lock when control enters and leaves the block of statements that follows.

Its become good practice to use it since it adds additional safety to your code, and makes file reading sections more prominent and readable (in my opinion), because when you use a with statement, you are essentially inside an indentation block.

The reason is obvious: The context manager closes for example an open file after leaving the context manager block. Using context managers give you extra security.

For example I had this code:

content = open('foo.txt').read()
os.unlink('foo.txt')

This code worked perfectly on Linux but failed on Windows since the file remained open and Windows caused an exception (file locked or something like that as far as I recall).

This error would not be possible with writing the code as

with open('foo.txt') as fp:
    content= fp.read()
os.unlink('foo.txt')

The file would be closed after reading the content and before trying to unlink() the file.

Because it's a good practice to close unused handles instead of waiting for GC.

By the way your point is true, no notable benefits using with in your context. And probably less than a millisecond of penalty due to the explicit closing using with.

The with statement is better because it will ensure you always close the file, even if an exception is raised.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top