You need to filter out directories; os.listdir()
lists all names in a given path. You can use os.path.isdir()
for this:
basepath = '/path/to/directory'
for fname in os.listdir(basepath):
path = os.path.join(basepath, fname)
if os.path.isdir(path):
# skip directories
continue
Note that this only filters out directories after following symlinks. fname
is not necessarily a regular file, it could also be a symlink to a file. If you need to filter out symlinks as well, you'd need to use not os.path.islink()
first.
On a modern Python version (3.5 or newer), an even better option is to use the os.scandir()
function; this produces DirEntry()
instances. In the common case, this is faster as the direntry loaded already has cached enough information to determine if an entry is a directory or not:
basepath = '/path/to/directory'
for entry in os.scandir(basepath):
if entry.isdir():
# skip directories
continue
# use entry.path to get the full path of this entry, or use
# entry.name for the base filename
You can use entry.is_file(follow_symlinks=False)
if only regular files (and not symlinks) are needed.
os.walk()
does the same work under the hood; unless you need to recurse down subdirectories, you don't need to use os.walk()
here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…