1

Im working on a saving system for user names right now. The current variable looks like this: name = input("What is your name"). I have it writing out to a text file.

I tried setting name as a variable without the input, and tried making the input the write function (idk why). No luck with either of those.

def welcome():
    os.system('cls' if os.name == "nt" else 'clear')
    print(color.color.DarkGray + "Welcome...")
    sleep(5)
    name = input("Enter Name Here: ")
    name1 = name
    saveUserInp = open("userNames.txt", 'w')
    with open ("userNames.txt") as f:
        f.write(name)
    sleep(5)
    print("Welcome",name1)
    sleep(5)
    menu()

Provided above is the code for the welcome function.

Traceback (most recent call last):
  File "main.py", line 54, in <module>
    welcome()
  File "main.py", line 21, in welcome
    f.write(name)
io.UnsupportedOperation: not writable

Provided is the actual error given. Line 54 is calling the welcome function, which breaks after I type in my name. Like 21 is the f.write function. I am not sure why it doesn't want to write it into the file.

1
  • 2
    You opened userNames.txt for writing, and wrote nothing into it. You then opened the same file for reading (which is the default if you don't specify a mode), and tried to write into that. Commented May 21, 2019 at 16:49

1 Answer 1

2

You should open the file specifying the open mode if it's different than read:

with open ("userNames.txt", "w") as f:
        f.write(name)

open with no mode provided opens the file in read mode by default, no surprise it's not writable.

By the way, what's the point of opening the file twice? Lines

saveUserInp = open("userNames.txt", 'w')
...
saveUserInp.close()

might be removed since you open file with the with statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I ran into another error. It wont write out into the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.