World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Shashank Shanu
a year 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