我想要在Python中做的是制作一个程序,您可以直接从命令行写入文件。我有所有的代码来做到这一点,但唯一相关的代码如下。文件命名,Python
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('mytext.txt', 'a')
outfile.write(textin)
我所要做的是让这样的人可以选择文件名,但是当我做输入功能,name = input("Choose your filename, don't include an extension: ")
和变线4成outfile = open(name, 'txt', 'a')
,我得到一个语法错误!
您可能打算连接名称,如'open(name +'.txt','a')'。你拥有它的方式会是一个问题,因为'“txt”'会被解释为'open()'的模式参数。 – birryree 2012-03-07 21:54:45
@birryree:请添加您的评论作为答案。 – Philipp 2012-03-07 21:56:10
好吧,我的评论可能已经发现了一个不同的问题,但是你的语法错误可能是其他问题 - 你可以更新以向我们展示当你尝试改变'outfile'行时你的代码是什么样的? – birryree 2012-03-07 22:07:43