416

How do you check if a path is a directory or file in python?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
duhhunjonn
  • 44,855
  • 11
  • 28
  • 15

4 Answers4

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
  • 13
    if 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
    os.path.isfile("1.mp4") gives me false! Why is that? – Jay Patel Jan 15 '19 at 06:23
  • 2
    that'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
    What if the file paths don't exist yet? Both functions return False. – cs95 Mar 25 '19 at 04:14
  • 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
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
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
  • 2
    Not 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