Python无法打印文本文件

99 阅读3分钟

这个问题与 Python 程序无法打印文本文件有关。在进行第 16 个学习练习的第二个练习时,使用 Python 3 的最新版本,遇到了一个问题。代码如下:

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()

当运行此代码时,在输入完三个文本行后,输入文件名并尝试读取它时,程序却没有打印出任何内容。

2、解决方案

  1. 确保在所有写入调用完成后关闭文件。由于操作系统缓冲区等原因,数据在关闭文件对象之前并不能保证实际写入到文件中(当你退出程序时,会自动执行此操作——你可能已经注意到了——终止程序,数据会保存在文件中)。
  2. truncate 调用是不必要的——以“w”模式打开文件会立即截断文件。

修改后的代码如下:

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 ("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")

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

target.close()  # close the file

现在,当运行此代码时,它将成功地读取并打印出之前输入的文本行。

BONUS 问题:在提到“look into error unexpected EOF while parsing, I had to type "", if I don't program ends here”时,为什么必须加上引号?如果不加引号,程序就会结束,并显示评论中提到的错误信息。

这是因为 input() 函数期望输入一个字符串。如果不加引号,Python 会将其解释为一个表达式,并尝试执行它。由于没有表达式可执行,因此会引发意外的 EOF 错误。添加引号可以确保将其解释为字符串,并允许程序继续运行。