banner

© 2004-19 ict4u.net

File Exists

To make your program able to cope with errors before trying to read a file you should check to see if it exists
The code snippet below uses the OS library to interrogate the file system of your computer

#
# Check to see if a files exists
#
import os  # make th OS library available

filename = 'FootballTeams.txt'
FileExists = os.path.exists(filename)  # use current working directory (folder) to see if the file is present
print(f"The File: {filename}  Exists: {FileExists}")