emanuele
emanuele

Reputation: 2589

Differences between `open(fname, 'r').close()` and `os.path.isfile(fname)`

I have to check the presence and readable of multiple files. Which is the most efficient way to do it?

list_of_files = [fname1,fname2,fname3]
for fname in list_of_files:
    try:
        open(fname, 'r').close()
    except IOError:
        raise YourCustomError

or

list_of_files = [fname1,fname2,fname3]
for fname in list_of_files:
    if not ps.path.isfile(fname):
        raise YourCustomError

Upvotes: 1

Views: 415

Answers (3)

Bakuriu
Bakuriu

Reputation: 102039

If you plan on using those files then neither. Files may be deleted or permissions changed between your call and when you use the file, making that information obsolete. Instead just try to use the file and handle the exception there:

try:
    with open(fname, 'r') as f:
        # use f
except FileNotFoundError as e:
    # file was deleted
except IsADirectoryError as e:
    # path exists but is a directory
except PermissionError as e:
    # you don't have permissions to read the file
except OSError as e:
    # other error

If however you are writing a tool that is displaying the information about permissions to the user, then it makes sense to use the methods and functions provided for specifically this purpose, hence you should use os.path.exists and os.is_file and os.is_dir.

If you are dealing with entire directories note that it is more efficient to use os.scandir and check the methods on the DirEntrys objects.

Upvotes: 2

Ami Tavory
Ami Tavory

Reputation: 76406

I have to check the presence and readable of multiple files. Which is the most efficient way to do it?

It's not just a question of efficiency, as your two approaches don't do the same thing. A file might exist and be unreadable to you, say, because of permissions. Your first approach is the only one of the two that actually checks that the file is open and readable (at least at some point in time - both your approaches have race conditions).

Upvotes: 1

masnun
masnun

Reputation: 11916

There is a common philosophy - execute and ask for forgiveness than asking for permission before hand.

The first approach tries to open the file in readable mode and fails if it doesn't exist or not readable. Here we're asking for forgiveness after the operation fails.

In the second approach, we are just asking for permission first. Your second example however just checks if it's a file, doesn't check permission.

However, instead of manually closing the file handler, using context managers (with open(filename, "r")) might be a much better approach.

Upvotes: 1

Related Questions