how to get content of a small ascii file in python?

Viewed 47250

Let's say we want to implement an equivalent of the PHP's file_get_content.

What is the best practice? (elegant and reliable)

Here are some proposition, are they correct?

using with statement:

def file_get_contents(filename):
    with file(filename) as f:
        s = f.read()
    return s

is using standard open() safe?

def file_get_contents(filename):
    return open(filename).read()

What happens to file descriptor in either solution?

5 Answers
Related