[Tutor] os.access vs os.path.isfile
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sat Aug 20 20:49:27 CEST 2005
> os.access is better/fast that os.path.isfile for checking if exist a file?
I don't think we should judge this primarily as a matter of speed, but a
matter of functionality. If we do something like:
os.access(pathname, os.F_OK)
this will tell us if a path name exists. But note that this doesn't
necessarily mean that pathname is a regular file: it can be a directory
too:
######
>>> import os
>>> os.access("/usr/share/dict/", os.F_OK)
True
>>> os.access("/usr/share/dict/words", os.F_OK)
True
>>> os.access("/foo", os.F_OK)
False
######
On the other hand, os.path.isfile() checks to see if we're looking at a
non-directory file:
######
>>> import os.path
>>> os.path.isfile("/usr/share/dict/")
False
>>> os.path.isfile("/usr/share/dict/words")
True
>>> os.path.isfile("/foo")
False
######
So they do different things.
The intent that you're trying to express is what you should use to choose
between the two: what do you really want to check for?
Hope this helps!
More information about the Tutor
mailing list