无法覆盖文件

时间:2017-06-23 17:10:39

标签: python python-3.x

我无法正常运行此练习:

from sys import argv

script, filename = argv

print(f"We're going to erase {filename}.")
print("If you don't want that, CRTL-C (^C).")
print("If you do want that, hit RETURN.")

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 = input("line 1: ")
line2 = input("line 2: ")
line3 = 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 finally, we close it")
target.close()

当我运行它时,它实际上并没有覆盖该文件。它会创建一个包含我输入的三行的新文件。我知道truncate应该清空文件,所以我不知道为什么要创建另一个文件。我不确定到底出了什么问题,让我知道。感谢

0 个答案:

没有答案