Created
September 14, 2011 06:32
-
-
Save adaptives/1215987 to your computer and use it in GitHub Desktop.
Writing to a file in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sys import argv | |
| script, filename = argv | |
| print "We're going to erase %r." % filename | |
| print "If you don't want that, hit CTRL-C (^C)." | |
| print "If you want that hit RETURN." | |
| raw_input("?") | |
| print "Opening the file..." | |
| target = open(filename, 'w') | |
| print "Truncating the file. Goodbye!" | |
| target.truncate() | |
| print "Now I'm going to ask you for three lines." | |
| line1 = raw_input("line 1: ") | |
| line2 = raw_input("line 2: ") | |
| line3 = raw_input("line 3: ") | |
| print "I'm going to write these to the file." | |
| target.write(line1) | |
| target.write("\n") | |
| target.write(line2) | |
| target.write("\n") | |
| target.write(line3) | |
| target.write("\n") | |
| print "And we finally, close it." | |
| target.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # LPTHW Exercise 16 | |
| # Import the argv variable from the sys module | |
| from sys import argv | |
| #unpack the contents of argv into script and filename | |
| script, filename = argv | |
| #create a general variable which we will re-use for the prompt | |
| prompt = "line %d: " | |
| # Tell the user that we are going to erase the filename they mentioned in the cmd argument | |
| print "We're going to erase %r." % filename | |
| # Prompt the user that they can hit CTEL-C if they do not want to erase the file | |
| print "If you don't want that, hit CTRL-C (^C)." | |
| #Prompt the user that they can hit RETURN if they DO want to erase the file | |
| print "If you want that hit RETURN." | |
| # Take input from the user... this will determine if we will erase the file or not | |
| raw_input("?") | |
| print "Opening the file..." | |
| #open the specified file and hold the fp in a variable called target | |
| target = open(filename, 'w') | |
| print "Truncating the file. Goodbye!" | |
| #truncate the file | |
| target.truncate() | |
| print "Now I'm going to ask you for three lines." | |
| #aAccept three lined from the user | |
| line1 = raw_input(prompt % 1) | |
| line2 = raw_input(prompt % 2) | |
| line3 = raw_input(prompt % 3) | |
| print "I'm going to write these to the file." | |
| #Write all the three lines to the file seperated with a newline | |
| target.write(line1) | |
| target.write("\n") | |
| target.write(line2) | |
| target.write("\n") | |
| target.write(line3) | |
| target.write("\n") | |
| print "And we finally, close it." | |
| # Close the file | |
| target.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
learn Python The Hard way 👍