我试图让我的用户输入保存但是,当我尝试这样做时,这个消息出现在python shell中:
nf.write('\n'.join(tempword))
io.UnsupportedOperation: not writable
以下是该部分的代码:
clues_list=[]
userInput=input("Enter a symbol you would like to replace:")
userInput2=input("What letter would you like to replace it with:")
for word in words_list:
tempword = (word)
tempword = tempword.replace('#','A')
tempword = tempword.replace('*', 'M')
tempword = tempword.replace('%', 'N')
tempword=tempword.replace(userInput,userInput2)
print(tempword)
clues_list.append(tempword)
with open('words.txt', 'r') as nf:# bit that isnt working
nf.write('\n'.join(tempword))
基本上,我希望显示用户输入,但不会发生这种情况。有人可以向我解释为什么以及我需要做些什么来解决它? 问候
答案 0 :(得分:3)
您似乎只是将words.txt
打开为只读,然后尝试写入它。请尝试改为:
with open('words.txt', 'w') as nf:
nf.write('\n'.join(tempword))
请注意,这会在写入文件之前将文件空白。如果您需要附加到文件末尾,请改用'a'
('append'
)模式。