首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的程序创建了一个新文件,但名称错误。我该如何解决这个问题?

我的程序创建了一个新文件,但名称错误。我该如何解决这个问题?
EN

Stack Overflow用户
提问于 2020-10-08 09:50:40
回答 2查看 31关注 0票数 0
代码语言:javascript
复制
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笔记本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-08 10:02:56

你已经在那个文件夹中有了一个new.txt,这就是为什么它将这个命名为new2.txt,删除或重命名第一个,然后尝试重新运行你的程序。

票数 0
EN

Stack Overflow用户

发布于 2020-10-08 10:55:34

您没有在正确的目录中查找,或者该目录已有同名的现有文件。还要检查您对该目录的写访问权限

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64254672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档