How you will check if the file exists in python or not? There are many ways to do know about file existence. Like a exists(),
One more is pathlibPath.exists() in Python 3.4 or above version.
See below the list of ways to verify a file or directory exists in python programming.
Let’s see the example of check if a file exists
1. First os.path.exists()
This method is existing in standard python libraries. And available on Python 2 and 3 versions. It will check the existence of a file or directory on a given path. let’s see the example/demo use of os.path.exists().
This method will return a Boolean result as true o false. If the
In this example we don’t a file, so it should return a false.
from os import path result = path.exists("eye.txt") print (result)
Must read tutorial how to import library in python- Python Import Module (Library/Package/File) System
2. os.path.isfile() – check if file exists
Another method but same as above. Let’s see the example and file “testFile.txt” is exists in the project folder.
from os import path result = path.isfile("testFile.txt") print (result)
Q: What is if you pass the directory name in “isfile” method?
Answer: It will return a false value because of
3. os.path.isdir() – check if directory exists
Now if you want the check given path is for directory then use “isdir()” method.
If its folder/Directory then return value is true, else return value will be false.
from os import path result = path.isdir("dir") print (result)
This method can use in Python 3.4 and above versions. This method is used a Object oriented programming approach.
Let’s see the one basic example of it. Using a if else condition statement.
import pathlib file = pathlib.Path("testFile.txt") if file.exists (): print ("File exist") else: print ("File not exist")
Output: File exist
Python File Handling Quiz
0 of 10 questions completed
Python File Handling to test your knowledge – ( Create, Open, Update, delete and more about in Python)
It’s for Beginners, Advanced and Experienced Programmers.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
0 of 10 questions answered correctly
Time has elapsed
You have reached 0 of 0 points, (0)
|Table is loading|
|No data available|
Question 1 of 10
Which of the following command is used to open a file “c:\textFile.txt” in read-mode only?Correct
Question 2 of 10
Which functions use to check if a file exists?
File name = “logo”Correct
Question 3 of 10
How do you insert something on a new line in a file?Correct
Question 4 of 10
Appending to a file means adding extra data into the file.Correct
Question 5 of 10
What is the last action that must be performed on a file?Correct
Question 6 of 10
What is the data type of data read from a file?Correct
Question 7 of 10
Can you create a file without using file modes in Python?Correct
Question 8 of 10
Reading from a file often involves using…Correct
Question 9 of 10
Which of the following statements are true regarding the opening modes of a file?Correct
Question 10 of 10
Which of the following commands option right to read the entire contents of a file as a string using the file object
Do comment if you have any doubts and suggestions on this tutorial. Post your interview question in the comment section.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples of Python check if file or Directory exists are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.