0
Check if a file exists using Python

How do I check if a file exists, using Python, without using a try statement?\r\n\r\n


Python 27-03-15, 12:26 p.m. test2
0
\r\n

You can use:

import os.path

os.path.isfile(fname)

\r\n\r\n\r\n\r\n

if you need to be sure it's a file.

\r\n
30-03-15, 3:55 p.m. test2



0
you can also use

import os.path
os.path.exists(fname)
07-05-15, 4:08 p.m. test



0
There is a FileNotFoundError exception in Python-3.
07-03-16, 2:49 p.m. thomaswhite544



0
I came from python 2 and I learnt a lot after readingthis great guide that shows how to check if file exists with python 2 and 3.
Hope it helps!
28-12-16, 9:37 a.m. yusef78



0
import os.path
filename = "my_file.txt"
if(os.path.isfile(filename)):
print("File Exists!!")
else:
print("File does not exists!!")
Full source...file exist
25-09-17, 3:51 p.m. grovalmitch




This question is closed to new answers