【发布时间】:2012-03-07 22:21:46
【问题描述】:
我正在用 Python 编写一个程序,您可以从该程序远程写入文本文件。相关代码:
namein = input("What do you want the filename to be? Don't include an extension...\n")
extin = input("What would you like the extension to be? This program supports:\nMicrosoft Word Document: '.docx'\nPlain Text File: '.txt'\nRich Text File: '.rtf'\nPages Document: '.pages'\n")
aw = input("Do you want to append your text file, or rewrite the whole thing? (append/write) ")
if aw == 'append':
textin = input("In the line below, write the text you want to put into your text document!\n\n")
outfile = open(namein + extin, 'a')
outfile.write(textin)
outfile.flush()
print("Great! Now your text file has been updated!")
print("Your text file:\n")
outfile.close()
outfile = open(namein + extin, 'r')
print(outfile.read())
当有人选择非“.txt”文件时,无法打开该文件!它只是说错误,文件无法打开。有没有办法解决这个问题?
【问题讨论】:
-
你让人们在服务器文件系统的任何地方创建任意文件?
-
是的,您需要以正确的输出格式将数据写入文件。
-
@sdolan: "远程" :) 我喜欢。
-
请给我们完整的错误信息。 “它”是什么?
-
@Karoly:我的猜测是当纯文本文件以
.doc或.pdf打开时,文件处理程序会显示错误...
标签: python file text-files