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 13, 2018 in Python by 93.lynn
• 1,550 points
59 views

5 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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 13, 2018 by anto.trigg4
• 3,420 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 in Python by Omkar
• 65,850 points
10 views
0 votes
1 answer

How does Python know whether a variable in the class is a method or a variable?

In python objects/variables are wrapped into methods ...READ MORE

answered Sep 18, 2018 in Python by ariaholic
• 7,320 points
20 views
0 votes
1 answer

Section postgresql not found in the database.ini file

Python doesn't know what $FILEDIR is. Try an absolute path ...READ MORE

answered Oct 3, 2018 in Python by Priyaj
• 56,120 points
35 views
0 votes
1 answer

Check a number is Prime or not in Python

There is an efficient way to write ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 56,120 points
57 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
27 views
+4 votes
6 answers
+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,420 points
43 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,420 points
972 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.