简体   繁体   中英

How do you write a list to a file in Python?

I'm a beginner in Python and ran across an error. I am trying to create a programme that will take a username and password made by a user, write them into lists and write those lists to files. Here is some of my code: This is the part where the user is creating a username&password.

userName=input('Please enter a username')

password=input('Please enter a password')

password2=input('Please re-enter your password')

if password==password2:

    print('Your passwords match.')

while password!=password2:

    password2=input('Sorry. Your passwords did not match. Please try again')

    if password==password2:

        print('Your passwords match')

My code works fine up until this point, where I get the error:

invalid file: <_io.TextIOWrapper name='usernameList.txt' mode='wt' encoding='cp1252'>.

I'm not sure why this error is being returned.

if password==password2:
    usernames=[]
    usernameFile=open('usernameList.txt', 'wt')
    with open(usernameFile, 'wb') as f:
        pickle.dump(usernames,f)
    userNames.append(userName)
    usernameFile.close()
    passwords=[]
    passwordFile=open('passwordList.txt', 'wt')
    with open(passwordFile, 'wb') as f:
        pickle.dump(passwords,f)

    passwords.append(password)
    passwordFile.close()

Is there any way to fix the error, or another way to write the lists to a file? Thanks

usernameFile=open('usernameList.txt', 'wt')
with open(usernameFile, 'wb') as f:

In the second line usernameFile is a file object. The first argument to open must be a file name ( io.open() also supports file descriptor numbers as ints). open() tries to coerce its argument to a string.

In your case, this results in

str(usernameFile) == '<_io.TextIOWrapper name='usernameList.txt' mode='wt' encoding='cp1252'>'

which is not a valid filename.

Replace with

with open('usernameList.txt', 'wt') as f:

and get rid of usernameFile completely.

You had the right idea, but there were a number of issues. When the user passwords do not match, normally you would prompt for both again.

The with block is designed to open and close your files, so there is no need to add a close at the end.

The script below shows what I mean, you will then have two files holding a Python list . So trying to view it will not make much sense, you will now need to write the corresponding read part to your code.

import pickle

userName = input('Please enter a username: ')

while True:
    password1 = input('Please enter a password: ')
    password2 = input('Please re-enter your password: ')

    if password1 == password2:
        print('Your passwords match.')
        break
    else:
        print('Sorry. Your passwords did not match. Please try again')

user_names = []
user_names.append(userName)

with open('usernameList.txt', 'wb') as f_username:
    pickle.dump(user_names, f_username)

passwords = []
passwords.append(password1)

with open('passwordList.txt', 'wb') as f_password:
    pickle.dump(passwords, f_password)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM