我对python中的truncate函数有些困惑。截断函数是否不应该清空文件并仅显示输入的第二行?但是在我的文件中,程序结束后第一和第二输入行都在文件中。
from sys import argv
script, filename = argv
print(f"Erasing the file {filename}")
print("Opening the file...")
# This empties and overrides the file when opening in w mode use 'a'to append to file
target = open(filename, 'w')
line = input("Input a line")
target.write(line)
print("Truncating ie erasing the file...")
target.truncate()
line2 = input("Input another line")
target.write(line2)
target.close()