Mega Code Archive

 
Categories / Python Tutorial / File
 

Function that tries to read a file and return its contents

def safe_read(filename):     f = open(filename)     try:         data = f.read()     except IOError:         data = None     finally:         f.close()     return data