bob
bob

Reputation: 1889

Overhead of passing around a file object instead of file name?

I have a method that detects what file it should open based on input, opens the file, then returns the file object.

def find_and_open(search_term):
    # ... logic to find file
    return open(filename, 'r')

I like this way because it hides the most amount of implementation from the caller. You give it your criteria, it spits out the file object. Why bother with String paths if you're just going to open it anyway?

However, in other Python projects I tend to see such a method return a string of the filepath, instead of the fileobject itself. The file is then opened at the very last minute, read/edited, and closed.

My questions are:

Upvotes: 4

Views: 1041

Answers (1)

Kyler Brown
Kyler Brown

Reputation: 1126

  1. Performance is unlikely to be an issue. Reading and writing to disk are orders of magnitude slower than reading from RAM, so passing a pointer around is very unlikely to be the performance bottleneck. 1
  2. From the python docs:

    It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks ...

Note that you can both use with to open a file and pass the file object around to other functions, either by nesting functions or using yield. Though I would consider this less pythonic than passing a file string around in most cases.

Simple is better than complex. Flat is better than nested.

You might also be interested in pathlib.

Upvotes: 3

Related Questions