I\'m trying to open a file, and if the file doesn\'t exist, I need to create it and open it for writing. I have this so far:
#open file for reading fn = input(\"
Using input() implies Python 3, recent Python 3 versions have made the IOError exception deprecated (it is now an alias for OSError). So assuming you are using Python 3.3 or later:
input()
IOError
OSError
fn = input('Enter file name: ') try: file = open(fn, 'r') except FileNotFoundError: file = open(fn, 'w')