简体   繁体   English

Python程序不打印文本文件

[英]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. 我正在阅读“努力学习python”一书,在第16个学习练习第二个练习中遇到了一个问题 I am using the newest version of python 3. 我正在使用最新版本的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: 此时,我想知道为什么“#默认代码已完成”之后的内容不起作用,我从另一个程序(同一本书的练习15)完全按原样撕了它,我的命令行看起来像这个:

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? 奖励问题:在我说的那一刻(“在分析时#进入错误意外EOF,如果我没有在此处结束编程,则必须键入””)为什么必须加上引号? 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. 完成所有write调用后, close()文件。 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). 由于诸如OS缓冲之类的事情,在关闭文件对象之前,不能保证将数据实际写入文件中(如您可能已经注意到的那样,在退出程序时该操作会自动完成-杀死程序,数据将被删除)在文件中)。

In addition, your truncate call is unnecessary -- opening a file in "w" mode immediately truncates it. 此外,您不需要进行truncate调用-以"w"模式打开文件会立即将其截断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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