简体   繁体   中英

Python program not printing text file

I'm working through the "Learn python the hard way" book, I've run into a problem in the Second Exercise of the 16th study drill. I am using the newest version of python 3.

The code I wrote looks like this so far:

from sys import argv

script, filename = argv #we input the filename into argv

print ("We're going to erase %r." % filename)   #tells us the name of the file we're deleting
print ("If you don't want that, hit CTRL-C (^C)")
print ("If you do want that, hit RETURN.")

input("?")  #look into error unexpected EOF while parsing, I had to type "", if I don't program ends here

print ("Opening the file...")
target = open(filename, "w")    #opens the file

print ("Truncating the file. Goodbye!")
target.truncate()   #erases everything in the file

print ("Now I'm going to ask you for three lines")

line1 = input("line 1: ")
line2 = input("line 2: ")
line3 = input("line 3: ")
    #summary: we create 3 input variables we'll use under here
print ("I'm going to write these to the file.")

target.write(line1) #summary: we write in the terminal what we want in our file and use that
target.write("\n")  #we also have this newline command after every line to make sure it's not all in one line
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

    #The default code is done at this point

print ("Type the filename again and I'll read it back to you or press CTRL + C (^C) to exit")
file_again = input(">")

print (file_again)  #Added to try to see what this gave me, it gives nothing back --- isn't working, why?

print ("Now I'll read the file back to you!")   #This prints
text_again = open(file_again)   #Not sure if this is working
print (text_again.read())   #Is doing nothing in the Terminal

print("And finally we close the file")  #Works
target.close()

At this point I'm wondering why the stuff after "#The default code is done at this point" is not working, I ripped it exactly as-is from another program (Exercise 15 of the same book), my Command line looks like this:

C:\PATH\Study Drills>py SD8.py "test.txt"
We're going to erase 'test.txt'.
If you don't want that, hit CTRL-C (^C)
If you do want that, hit RETURN.
?""
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines
line 1: "Hey"
line 2: "yo"
line 3: "sup"
I'm going to write these to the file.
Type the filename again and I'll read it back to you or press CTRL + C (^C) to exit
>"test.txt"
Now I'll read the file back to you!

BONUS QUESTION: In the point where I say ("#look into error unexpected EOF while parsing, I had to type "", if I don't program ends here") why do I have to put quotation marks? If I don't the program ends with the error message mentioned in the comment.

Once you're done with all your write calls, close() the file. Due to things like OS buffering, the data is not guaranteed to be actually written to the file until you close the file object (which is done automatically when you exit the program, as you might have noticed -- kill your program and the data will be in the file).

In addition, your truncate call is unnecessary -- opening a file in "w" mode immediately truncates it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM