Python
Python Vs PHP: What's the Difference?
What is Python? Python is a high level object-oriented, programming language. It has built-in data...
Python exists() method is used to check whether specific file or directory exists or not. It is also used to check if a path refers to any open file descriptor or not. It returns boolean value true if file exists and returns false otherwise. It is used with os module and os.path sub module as os.path.exists(path).
In this tutorial, we will learn how to determine whether a file (or directory) exists using Python. To check this, we use Built-in library functions.
There are different ways to verify a file or directory exists, using functions as listed below.
Using path.exists you can quickly check that a file or directory exists. Here are the steps
Steps 1) Before you run the code, it is important that you import the os.path module.
import os.path from os import path
Steps 2) Now, use the path.exists() function to check whether a File Exists.
path.exists("gtupapers.txt")Steps 3) Here is the complete code
import os.path
from os import path
def main():
print ("File exists:"+str(path.exists('gtupapers.txt')))
print ("File exists:" + str(path.exists('career.gtupapers.txt')))
print ("directory exists:" + str(path.exists('myDirectory')))
if __name__== "__main__":
main()In our case only file gtupapers.txt is created in the working directory
Output:
File exists: True
File exists: False
directory exists: False
The Python isfile() method is used to find whether a given path is an existing regular file or not. It returns a boolean value true if the specific path is an existing file or else it returns false. It can be used by the syntax : os.path.isfile(path).
We can use the isfile command to check whether a given input is a file or not.
import os.path
from os import path
def main():
print ("Is it File?" + str(path.isfile('gtupapers.txt')))
print ("Is it File?" + str(path.isfile('myDirectory')))
if __name__== "__main__":
main()Output:
Is it File? True
Is it File? False
If we want to confirm that a given path points to a directory, we can use the os.path.dir() function
import os.path
from os import path
def main():
print ("Is it Directory?" + str(path.isdir('gtupapers.txt')))
print ("Is it Directory?" + str(path.isdir('myDirectory')))
if __name__== "__main__":
main()Output:
Is it Directory? False
Is it Directory? True
Python 3.4 and above versions have pathlib Module for handling with file system path. It used object-oriented approach to check if file exist or not.
import pathlib
file = pathlib.Path("gtupapers.txt")
if file.exists ():
print ("File exist")
else:
print ("File not exist")
Output:
File exist
Complete Code
Here is the complete code
import os
from os import path
def main():
# Print the name of the OS
print(os.name)
#Check for item existence and type
print("Item exists:" + str(path.exists("gtupapers.txt")))
print("Item is a file: " + str(path.isfile("gtupapers.txt")))
print("Item is a directory: " + str(path.isdir("gtupapers.txt")))
if __name__ == "__main__":
main()
Output:
Item exists: True
Item is a file: True
Item is a directory: False
os.path.exists() – Returns True if path or directory does exists.os.path.isfile() – Returns True if path is File.os.path.isdir() - Returns True if path is Directory.pathlib.Path.exists() - Returns True if path or directory does exists. (In Python 3.4 and above versions)
What is Python? Python is a high level object-oriented, programming language. It has built-in data...
In this tutorial of difference between Python and JavaScript, we will discuss the key differences...
What is Lambda Function in Python? A Lambda Function in Python programming is an anonymous...
len() is a built-in function in python. You can use the len() to get the length of the given...
Round() Round() is a built-in function available with python. It will return you a float number that...
What is Unit Testing? Unit Testing in Python is done to identify bugs early in the development stage of...