Check whether the file exists or not

+1 vote
Without using the try statement, how can I see whether my file exists or not?
Apr 14, 2018 in Python by 93.lynn
• 1,600 points
1,964 views

5 answers to this question.

+1 vote

Simply import the "os" module and using the "exists" function, it will return a boolean value as True/ False. Write the below command:

print(os.path.exists("path"))

You can also check whether the desired path is a directory or a file. For that, use the below command:

print(os.path.isdir("path"))
answered Apr 14, 2018 by anto.trigg4
• 3,440 points
+1 vote

you can use os.path.isfile

answered Oct 18, 2018 by abc
+1 vote

You have the os.path.exists function:

import os.path
os.path.exists(file_path)

This returns True for both files and directories but you can instead use

os.path.isfile(file_name)

to test if it's a file specifically. It follows symlinks.

answered Oct 18, 2018 by Nabarupa
+1 vote

You can use the following command to check the availability of your file

import os.path
if os.path.isfile(filepath):
answered Oct 18, 2018 by Hemanth
+1 vote

Use this

import os
os.path.exists(path) # Returns whether the path (directory or file) exists or not
os.path.isfile(path) # Returns whether the file exists or not
answered Oct 18, 2018 by reyam

Related Questions In Python

0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,866 views
0 votes
1 answer

Open a file if it exists or create and open it does not exists - Python

You can use this: f = open("ex.txt","w+") Here the ...READ MORE

answered Jun 20, 2019 in Python by Jason
7,203 views
0 votes
2 answers

What is the python logic to check if a number is armstrong number or not?

a = int (input('enter number')) num = a sum ...READ MORE

answered Jul 16, 2020 in Python by amrut
• 240 points
1,772 views
0 votes
1 answer

What is the logic to check if a number is prime or not in python?

n = int (input ('ENTER NUMBER TO ...READ MORE

answered Jul 16, 2020 in Python by amrut
• 240 points
1,070 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,305 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
4,881 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP