0123456
0123456

Reputation: 73

Saving user inputs so that they appear again - python 3.2

I am trying to get my user input to save however, when i have tried doing so, this message comes up in the python shell:

 nf.write('\n'.join(tempword))
io.UnsupportedOperation: not writable

Here is my code for that section:

 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))

Basically, i want the user input to be displayed however that isnt happening. Can someone please explain to me why and what i need to do to fix it ? Regards

Upvotes: 1

Views: 79

Answers (1)

Adam Smith
Adam Smith

Reputation: 54193

Looks like you're opening words.txt as read only, then trying to write to it. Try instead:

with open('words.txt', 'w') as nf:
    nf.write('\n'.join(tempword))

Note that this will blank your file before writing to it. If you need to append to the end of your file, use the 'a' ('append') mode instead.

Upvotes: 3

Related Questions