我用泡菜正确吗?Python

2024-09-27 09:36:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者,因此不确定为什么会收到以下错误:

TypeError: invalid file: []

对于这行代码:

usernamelist=open(user_names,'w')

我正在尝试输入用户名和密码,将它们写入文件,然后读取它们。在

下面是我剩下的代码:

user_names=[]
passwords=[]
username=input('Please enter a username')
password=input('Please enter a password')
usernamelist=open(user_names,'w')
pickle.dump(userName,usernamelist)
usernamelist.close()
usernamelist=open(user_names,'r')
loadusernames=pickle.load(usernamelist)

passwordlist=open(passwords,'w')
pickle.dump(password,passwordlist)
passwordlist.close()
passwordlist=open(passwords,'r')
loadpasswords=pickle.load(passwordlist)

所有答案将不胜感激。谢谢。在


Tags: 代码closeinputnamesusernamepasswordopendump
1条回答
网友
1楼 · 发布于 2024-09-27 09:36:39

根据你的脚本,这可能会有所帮助。它创造了一个'用户名.txt'和'密码.txt'存储输入用户名和密码。在

我使用python2.7,输入在python2.7和python3.x中的行为不同

"""
opf: output file
inf: input file

use with instead of .open .close: http://effbot.org/zone/python-with-statement.htm

for naming rules and coding style in Python: https://www.python.org/dev/peps/pep-0008/
"""


import pickle

username = raw_input('Please enter a username:\n')
password = raw_input('Please enter a password:\n')

with open('username.txt', 'wb') as opf:
    pickle.dump(username, opf)

with open('username.txt') as inf:
    load_usernames = pickle.load(inf)
    print load_usernames

with open('password.txt', 'wb') as opf:
    pickle.dump(password, opf)

with open('password.txt') as inf:
    load_passwords = pickle.load(inf)
    print load_passwords

相关问题 更多 >

    热门问题