Download our e-book of Introduction To Python
Shashank Shanu
2 years ago
import os.path
os.path.exists(“InsideAIML_directory/myfile.txt”)
output: True
os.path.exists(“myfile_copy.txt”)
output: False
os.path.exists(“InsideAIML_directory”)
True
import os.path
os.path.isfile (“InsideAIML_directory/myfile.txt”)
output: True
os.path.isfile (“myfile_copy.txt”)
output: False
os.path.isfile (“InsideAIML_directory”)
False
Open(“myfile_copy.txt”)
FileNotFoundError:
“[Errno 2] No such file or
directory: ‘myfile_copy.txt’ ”
try:
file = open(“myfile.txt”)
file.close()
except FileNotFoundError:
print(“The given file does not exist”)
try:
file = open(“myfile.txt”)
file.close()
except IOError:
print(“You cannot access this file”)
print(“You can access this file”)
def file_accessible(path,
mode= ’r’):
“””
Check if the file or directory at “path” can
be accessed by the program using the
“mode”
open plays equal to reading mode (r).
“””
try:
file = open(path, mode)
file.close()
except IOError:
return False
return True