0 votes
1 view
in Python by (32.1k points)

If you read an entire file with content = open('Path/to/file', 'r').read() is the filehandle left open until the script exits? Is there a more concise method to read a whole file?

1 Answer

0 votes
by (14.5k points)

For reading the entire file in Python you can use the following piece of code which will be the better solution:-

with open('Path/to/file', 'r') as content_file:

content = content_file.read()

This will always close the file immediately after the block ends; even if an exception occurs.

...