0 votes
1 view
in Python by (107k 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 (47.7k 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.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 14 in Python by Sammy (47.7k points)
0 votes
1 answer
asked Sep 25 in Python by Sammy (47.7k points)
0 votes
1 answer
asked Aug 27 in Python by Sammy (47.7k points)
0 votes
1 answer
asked Aug 23 in Python by Sammy (47.7k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...