如何在 python 中向用户询问输出文件
我必须向用户询问输出文件,然后将数据附加到其中,但每当我尝试时,它都会告诉我该数据没有附加属性。我认为这是因为当我尝试打开文件时,它会将其视为字符串,而不是附加数据的实际文件。我已经尝试了多种方法来执行此操作,但现在我只剩下这个:
Output_File = str(raw_input("Where would you like to save this data? "))
fileObject = open(Output_File, "a")
fileObject.append(Output, '\n')
fileObject.close()
我试图附加到它的输出只是我之前定义的列表。任何帮助将不胜感激。
I have to ask the user for an output file and then append data to it but whenever I try it tells me that the data has no append attribute. I think this is because when I try to open the file it is seeing it as a string and not an actual file to append data too. I have tried multiple ways of doing this but right now I am left with this:
Output_File = str(raw_input("Where would you like to save this data? "))
fileObject = open(Output_File, "a")
fileObject.append(Output, '\n')
fileObject.close()
The output that I am trying to append to it is just a list I earlier defined. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的错误在这一行:
使用文件对象的 write 方法:
Your error is at this line:
Use the write method of a file object:
文件对象没有
append
方法。您正在寻找write
。另外,str(raw_input(...))是多余的,raw_input已经返回一个字符串。File objects have no
append
method. You're looking forwrite
. Also,str(raw_input(...))
is redundant,raw_input
already returns a string.该错误消息非常不言自明。这是因为文件对象没有
append
方法。您应该简单地使用write
:The error message is pretty self-explanatory. This is because file objects don't have
append
method. You should simply usewrite
:只需使用 .write 方法即可。
just use the .write method.