我的程序创建了一个新文件,但名称错误。我该如何解决?
My program makes a new file but with the wrong name. How do I fix this?
import os.path
def start():
if os.path.exists("new.txt") == False:
Account = input('Username: ')
Password_for_account = input('Password: ')
all_the_content = []
all_the_content.append(Account)
all_the_content.append(Password_for_account)
else:
with open("new.txt") as file:
content = file.read()
all_the_content = content.strip('][').split(', ')
while True:
User = input('Username: ')
Password = input('Password: ')
if (User, Password) == all_the_content[0,1] :
print('Done')
break
else:
print('Invalid Password')
continue
return 'You have been succesfully logged in!'
print(start())
with open("new.txt") as file:
content = file.read()
all_the_content = content.strip('][').split(', ')
def add_new_pass():
new_username = input('Enter the new username: ')
new_password = input('Enter the new password: ')
all_the_content.append(new_username)
all_the_content.append(new_password)
return 'Your password has been added'
def check_pass(username):
position = all_the_content.index(username)
if username in all_the_content and position%2 == 0:
return 'The password for '+ username, 'is '+ all_the_content[position + 1]
else:
return 'Invalid username'
print(add_new_pass())
a = input()
print(check_pass(a))
with open ('new.txt', 'w') as file:
file.write(str(all_the_content))
我是初学者。每次我将文件设置为“new.txt”时,这段代码都会按预期执行所有操作,除了一件事。任何帮助表示赞赏!
它将其存储为“new2.txt”。我正在使用 Jupyter 笔记本。
您在那个文件夹中已经有一个 new.txt,这就是为什么它将这个命名为 new2.txt,擦除或重命名第一个并尝试 运行 重新编程。
您没有在正确的目录中查找,或者目录中已有同名文件。同时检查您对目录的写入权限
import os.path
def start():
if os.path.exists("new.txt") == False:
Account = input('Username: ')
Password_for_account = input('Password: ')
all_the_content = []
all_the_content.append(Account)
all_the_content.append(Password_for_account)
else:
with open("new.txt") as file:
content = file.read()
all_the_content = content.strip('][').split(', ')
while True:
User = input('Username: ')
Password = input('Password: ')
if (User, Password) == all_the_content[0,1] :
print('Done')
break
else:
print('Invalid Password')
continue
return 'You have been succesfully logged in!'
print(start())
with open("new.txt") as file:
content = file.read()
all_the_content = content.strip('][').split(', ')
def add_new_pass():
new_username = input('Enter the new username: ')
new_password = input('Enter the new password: ')
all_the_content.append(new_username)
all_the_content.append(new_password)
return 'Your password has been added'
def check_pass(username):
position = all_the_content.index(username)
if username in all_the_content and position%2 == 0:
return 'The password for '+ username, 'is '+ all_the_content[position + 1]
else:
return 'Invalid username'
print(add_new_pass())
a = input()
print(check_pass(a))
with open ('new.txt', 'w') as file:
file.write(str(all_the_content))
我是初学者。每次我将文件设置为“new.txt”时,这段代码都会按预期执行所有操作,除了一件事。任何帮助表示赞赏! 它将其存储为“new2.txt”。我正在使用 Jupyter 笔记本。
您在那个文件夹中已经有一个 new.txt,这就是为什么它将这个命名为 new2.txt,擦除或重命名第一个并尝试 运行 重新编程。
您没有在正确的目录中查找,或者目录中已有同名文件。同时检查您对目录的写入权限