0

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

tmdavison
  • 50,222
  • 11
  • 138
  • 131
Davina
  • 13
  • 1
  • 5
  • 1
    possible duplicate of [Python: Write a list to a file](http://stackoverflow.com/questions/899103/python-write-a-list-to-a-file) – Andy Aug 24 '15 at 15:05
  • Also, your only retrying `password2`. What if the user typed in `password1` wrong? – muddyfish Aug 24 '15 at 15:06
  • 1
    You are opening the file simultaneously in 2 modes `wt` and `wb`, I guess `wb` is the required one here. – ZdaR Aug 24 '15 at 15:07
  • Thanks, but even opening the file completely in wb, I still get the error: – Davina Aug 24 '15 at 15:12
  • 1
    That `while` loop has a pretty big flaw. It only asks for the confirmation of the password. If the mistake is in the first version you will never be able to exit the loop. – kylieCatt Aug 24 '15 at 15:20

2 Answers2

1
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.

dhke
  • 13,716
  • 2
  • 34
  • 54
  • Thanks for the answer :) However now I am faced with a 'NameError: name 'pickle' is not defined' – Davina Aug 24 '15 at 15:29
  • @Davina This is a new question, then. Also as a hint for future questions: When encountering such errors it is always wise to post the full stack trace, because the contains the reference to the line of code causing the problem in the first place. – dhke Aug 24 '15 at 17:04
1

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)
Martin Evans
  • 37,882
  • 15
  • 62
  • 83