[英]TypeError: coercing to Unicode: need string or buffer, file found in python
[英]Getting Error coercing to Unicode: need string or buffer, file found after adding few lines in the file
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()
我试图截断文件,然后在其中写几行,之后,当我关闭文件并尝试重新打开它时,出现以下错误
错误在突出显示的部分..您能帮我什么可能是错误
之所以出现此错误,是因为open正在寻找文件名,并将文件对象本身传递给它。 从开放功能的文档 :
前两个参数与stdio的fopen()相同:name是要打开的文件名,mode是指示如何打开文件的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.