1import os
2
3os.path.exists("file.txt") # Or folder, will return true or false
1import os.path
2
3if os.path.isfile('filename.txt'):
4 print ("File exist")
5else:
6 print ("File not exist")
7
1import os.path
2
3if os.path.isfile('filename.txt'):
4 print ("File exist")
5else:
6 print ("File not exist")
1import os.path
2
3if os.path.exists('filename.txt'):
4 print ("File exist")
5else:
6 print ("File not exist")
1#using pathlib
2from pathlib import Path
3
4file_name = Path("file.txt")
5if file_name.exists():
6 print("exists")
7else:
8 print("does not exist")
1# Python program to explain os.path.exists() method
2
3# importing os module
4import os
5
6# Specify path
7path = '/usr/local/bin/'
8
9# Check whether the specified
10# path exists or not
11isExist = os.path.exists(path)
12print(isExist)
13
14
15# Specify path
16path = '/home/User/Desktop/file.txt'
17
18# Check whether the specified
19# path exists or not
20isExist = os.path.exists(path)
21print(isExist)
22
23# credit to geeksforgeeks.com at
24# https://www.geeksforgeeks.org/python-check-if-a-file-or-directory-exists/