How do you check if a path is a directory or file in python?
Asked
Active
Viewed 4.8e+01k times
416
-
if you are using the pathlib library do: `p.is_file()` see https://stackoverflow.com/a/44228884/1601580 – Charlie Parker Feb 28 '22 at 23:13
4 Answers
687
os.path.isfile("bob.txt") # Does bob.txt exist? Is it a file, or a directory?
os.path.isdir("bob")

Jesse Jashinsky
- 10,313
- 6
- 38
- 63
-
13if you are not using absolute paths, like in the example, it would only check for "bob"s existence in the script directory (or where python is currently situated in the filesystem) – Matthias Jun 21 '16 at 14:08
-
1
-
2that's because you aren't using full path there. use `os.path.isfile("/path/to/1.mp4")` – Manoj Jan 29 '19 at 07:32
-
1
-
2@cs95, You're going to have to check if the file exists first, and then check if it is a directory. If a file doesn't exist, it can't be a directory anyway! You're looking for os.path.exists: https://docs.python.org/3/library/os.path.html#os.path.exists – b4ux1t3 Feb 12 '20 at 14:06
-
@b4ux1t3 actually i got a case where isfile is False but isdir is True – Lightsout Feb 18 '21 at 00:53
-
@bakalolo Yeah, that's because I wasn't specific enough. isfile checks if something that exists is a file, isdir checks if something that exists is a directory. I specifically pointed out os.path.exists, which returns true if there is _anything_ at the path specified. If you check if the path exists at all, you can then check if it is specifically a directory. If a path exists and it is not a directory, it is a file. In any case, you can check if something exists and what it is in exactly two method calls. – b4ux1t3 Mar 01 '21 at 22:57
-
if you are using the pathlib library do: `p.is_file()` see https://stackoverflow.com/a/44228884/1601580 – Charlie Parker Feb 28 '22 at 23:13
147
use os.path.isdir(path)
more info here http://docs.python.org/library/os.path.html

Jordan
- 4,928
- 4
- 26
- 39
-
1If I use this to determine if something is a *file*, what if the thing at `path` is a symbolic link? – alex Aug 15 '17 at 18:19
-
This doesn't appear to work with file descriptors when using process substitution. – CMCDragonkai May 30 '18 at 05:26
-
if you are using the pathlib library do: `p.is_file()` see https://stackoverflow.com/a/44228884/1601580 – Charlie Parker Feb 28 '22 at 23:13
74
Many of the Python directory functions are in the os.path
module.
import os
os.path.isdir(d)

Chris B.
- 85,731
- 25
- 98
- 139
-
if you are using the pathlib library do: `p.is_file()` see https://stackoverflow.com/a/44228884/1601580 – Charlie Parker Feb 28 '22 at 23:14
22
An educational example from the stat documentation:
import os, sys
from stat import *
def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname
def visitfile(file):
print 'visiting', file
if __name__ == '__main__':
walktree(sys.argv[1], visitfile)

YuppieNetworking
- 8,672
- 7
- 44
- 65
-
2Not the prettiest solution, but if you've already got a stat structure, this allows you to avoid making an extra system call / disk seek via os.path.isfile or friends. – Joshua Richardson May 12 '14 at 18:39