Quantcast
Viewing latest article 5
Browse Latest Browse All 5

Check to see if file exists is failing

Here is my code:

# header.pydef add_header(filename):    header = '"""\nName of Project"""'    try:        f = open(filename, 'w')    except IOError:        print "Sorry could not open file, please check path"    else:        with f:            f.seek(0,0)            f.write(header)        print "Header added to", filenameif __name__ == "__main__":    filename = raw_input("Please provide path to file: ")    add_header(filename)

When I run this script (by doing python header.py), even when I provide a filename which does not exist it does not return the messages in the function. It returns nothing even when I replace the print statements with return statements. How would I show the messages in the function?


Viewing latest article 5
Browse Latest Browse All 5

Trending Articles