Note that there are some explanatory texts on larger screens.

plurals
  1. POPython program not printing text file
    primarykey
    data
    text
    <p>I'm working through the "Learn python the hard way" book, I've run into a problem in the <a href="http://learnpythonthehardway.org/book/ex16.html" rel="nofollow">Second Exercise of the 16th study drill.</a> I am using the newest version of python 3.</p> <p>The code I wrote looks like this so far:</p> <pre><code>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("&gt;") 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() </code></pre> <p>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:</p> <pre><code>C:\PATH\Study Drills&gt;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 &gt;"test.txt" Now I'll read the file back to you! </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload