Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a file is a directory or regular file in python? [duplicate]

Tags:

python

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

like image 654
duhhunjonn Avatar asked Jul 08 '10 14:07

duhhunjonn


People also ask

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

os. path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows a symbolic link, which means if the specified path is a symbolic link pointing to a directory then the method will return True.

How do you check if a file is a file or directory?

To check if the path you have is a file or directory, import os module and use isfile() method to check if it is a file, and isdir() method to check if it is a directory.

How do you check if a file is in a specific folder Python?

In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.

What is a regular file Python?

A regular file is one type of file stored in a file system. It is called "regular" primarily to distinguish it from other special types of files. Most files used directly by a human user are regular files. For example, executable files, text files, and image files are regular files.


2 Answers

os.path.isfile("bob.txt") # Does bob.txt exist?  Is it a file, or a directory? os.path.isdir("bob") 
like image 144
Jesse Jashinsky Avatar answered Oct 15 '22 15:10

Jesse Jashinsky


use os.path.isdir(path)

more info here http://docs.python.org/library/os.path.html

like image 27
Jordan Avatar answered Oct 15 '22 14:10

Jordan