Reputation: 3670
I took a look at the source codes. glob.glob
uses os.listdir
and fnmatch
to filter the file path. os.path.isfile
tries to get file stat. However, I didn't find the source code for os.listdir
and don't know how it's implemented.
When checking if a file exists, is os.path.isfile
much faster than glob.glob
since os.listdir
takes time to list all of them?
Upvotes: 0
Views: 767
Reputation: 689
glob.glob('./')
essentially does what ls *
would do on the command line. os.path.isfile
needs a specific file handle in order to work, this means it will usually be faster than glob, simply because there will be less operations and glob does not return a boolean value. Here is a sample timed in my working directory
with glob:
In [2]: %timeit glob.glob('./')
The slowest run took 15.81 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 9.8 μs per loop
with listdir:
In [5]: %timeit os.path.isfile('./')
The slowest run took 24.48 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 4.04 μs per loop
Upvotes: 1