diff options
author | Dinko Galetic <dgaletic@everflame.(none)> | 2010-06-09 22:42:54 (GMT) |
---|---|---|
committer | Dinko Galetic <dgaletic@everflame.(none)> | 2010-06-09 22:42:54 (GMT) |
commit | cc0bd9699b0d513450c6f9bb8bd8fe9341cfbd01 (patch) | |
tree | c863d037c6b0813dcd9dac7bb428a5379977331b /data | |
parent | ee49fee0f500ea8adda0f780efe396788b4beb83 (diff) |
Several fixes to file r/w example.
1) appends / to the end of the file path if the user forgot.
2) displays warning and asks for overwrite confirmation of the output file already exists.
3) exceptions caught while reading/writing files.
4) a small grammar correction.
Diffstat (limited to 'data')
-rw-r--r-- | data/GSOC examples/file writing and reading | 64 |
1 files changed, 42 insertions, 22 deletions
diff --git a/data/GSOC examples/file writing and reading b/data/GSOC examples/file writing and reading index 13b7436..bc01e01 100644 --- a/data/GSOC examples/file writing and reading +++ b/data/GSOC examples/file writing and reading @@ -1,41 +1,61 @@ import time +import os +import sys print "This example demonstrates how to create a text file, write something to it and then read what we wrote." -print "How shall we call the file?" +print "What shall we call the file?" filename = raw_input() print "Where would you like to store the file? (can be left empty)" # Practice: Where does the file get stored if we leave this empty? directory = raw_input() +if len(directory) != 0 and directory[-1] != "/": + directory = directory + "/" full_name = directory + filename - +if os.path.exists(full_name): + print "Warning: That file already exists, will be overwritten if you continue!" + print "Continue? (yes/no)" + cont = raw_input() + if cont == "no": + print "Aborting..." + sys.exit() + elif cont != "yes" and cont != "no": + print "Wrong input, aborting..." + sys.exit() + print "Please enter the text you would like to store." text = raw_input() -# Open the file in the "w"rite mode. -my_file = open(full_name, "w") - -# Let's write the current time to that file. +# Let's also write the current time to that file. # \n is the way of saying "i'm finished with that line, move to the next one." current_time = time.ctime() -my_file.write("The following text was written at: " + current_time + "\n") - -# Write our text to the open file. -my_file.write(text) -my_file.write("\n") - -# Close the file. -my_file.close() - -# Now lets read from that same file. This is how reading is done. -# First: Open the file in "r"ead mode. -my_file = open(full_name, "r") -# Second: Read from it. -from_file = my_file.read() -# Third: Close the file. -my_file.close() + +try: + # Open the file in the "w"rite mode. + my_file = open(full_name, "w") + # Write the current time: + my_file.write("The following text was written at: " + current_time + "\n") + # Write our text: + my_file.write(text) + my_file.write("\n") + # Close the file. + my_file.close() +except: + print "An error occured in the writing phase!" + +try: + # Now lets read from that same file. This is how reading is done. + # First: Open the file in "r"ead mode. + my_file = open(full_name, "r") + # Second: Read from it. + from_file = my_file.read() + # Third: Close the file. + my_file.close() +except: + print "An error occured in the reading phase!" + # Print what we've read. print from_file |