python-check-if-file-exists

Python Check If File Exists

Python exists()

Python exists() method is utilized to check if explicit record or registry exists. It is likewise used to check if a way alludes to any open record descriptor or not. It returns boolean worth valid if record exists and returns bogus something else. It is utilized with os module and os.path sub module as os.path.exists(path).

In this instructional exercise, we will figure out how to decide if a document (or catalog) exists utilizing Python. To check this, we utilize Built-in library capacities.

There are different ways to verify a file or directory exists, using functions as listed below.

  • os.path.exists()
  • os.path.isfile()
  • os.path.isdir()
  • pathlibPath.exists()

os.path.exists()

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("bigdata-world")

Steps 3) Here is the complete code

import os.path
from os import path

def main():

   print ("File exists:"+str(path.exists('bigdata-world')))
   print ("File exists:" + str(path.exists('career.bigdata-world')))
   print ("directory exists:" + str(path.exists('myDirectory')))

if __name__== "__main__":
   main()

In our case only file bigdata-world is created in the working directory

Output:

File exists: True
File exists: False
directory exists: False

Python isfile()

The Python isfile() method is utilized to discover whether a given way is a current customary document or not. It restores a boolean worth valid if the particular way is a current document or, in all likelihood it returns bogus. It tends to be utilized by the linguistic structure : os.path.isfile(path).

os.path.isfile()

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('bigdata-world')))
	print ("Is it File?" + str(path.isfile('myDirectory')))
if __name__== "__main__":
	main()

Output:

Is it File? True
Is it File? False

os.path.isdir()

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('bigdata-world')))
   print ("Is it Directory?" + str(path.isdir('myDirectory')))

if __name__== "__main__":
   main()

Output:

Is it Directory? False
Is it Directory? True

pathlibPath.exists() For Python 3.4

Python 3.4 or more forms have pathlib Module for taking care of with document framework way. It utilized article situated way to deal with check if record exist or not.

import pathlib
file = pathlib.Path("bigdata-world")
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("bigdata-world")))
print("Item is a file: " + str(path.isfile("bigdata-world")))
print("Item is a directory: " + str(path.isdir("bigdata-world")))

if __name__ == "__main__":
    main()

Output:

Item exists: True
Item is a file: True
Item is a directory: False

How to check If File Exists

  • 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)

Related Posts