from sys import argv
script,filename = argv
print "Will truncate the file : %s" %filename
print "If you wish to continue press \"Return\" or press CNTRL-C"
raw_input("?")
target = open(filename,"w")
print "We are truncating the file"
target.truncate()
print "File is truncated"
print "Now enter the three lines to add into the file"
line1 = raw_input("Line_1 : ")
line2 = raw_input("Line_2 : ")
line3 = raw_input("Line_3 : ")
print "Adding these line is in progress"
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print "Writter is completed"
target.close()
**print "Here is the new file :\n",open(target,mode='r', buffering=-1).read()**
target.close()
我正在尝试截断一个文件,然后在其中写入几行,之后当我关闭文件并尝试重新打开它时,它给了我以下错误
错误出现在突出显示的部分..你能帮我解决一下吗? 是错误
答案 0 :(得分:0)
您收到此错误,因为open正在查找文件名,并且您将文件对象本身传递给它。 From the docs on open function:
前两个参数与stdio的fopen()相同:name是 要打开的文件名,mode是一个表示如何的字符串 文件将被打开。