Posted By

GabrielTheodoulos on 07/20/16


file io os

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


Search to see if a File Exists

 / Published in: Python


The os module is to help you deal with any operation dealing with the directory.

  1. import os
  3. # todo check for existing file.
  4. if os.path.isfile('./gradebook.dat'): # check to see if file exists
  5. data = open('./gradebook.dat', 'rb') # if file exists open, read all data.
  6. data.close()
  7. else:
  8. print("The file gradebook.dat was not found.\n")
  9. print("We will create one....\n")

Report this snippet  

You need to login to post a comment.