python - Errno 2 没有这样的文件或目录“即使有一行可以工作的代码,我也会收到这个错误”
问题描述
我刚开始使用python,我想用文本文件制作一个简单的登录系统。每次我运行代码时都会出现此错误。它甚至不制作文本文件。在此之前,我可以运行我的代码并创建一个文件,但现在它没有。我只尝试了一行代码来打开一个文本文件,但这也不起作用。(代码行:f = open(“demofile.txt”))我也尝试用谷歌搜索它,没有解决方案。我不知道该怎么办?
def AskForAccount():
status = input("Do you have an account? ")
if status == "yes":
logIn()
elif status == "no":
createAccount()
else:
print("Type yes or no, please.")
AskForAccount()
def createAccount():
name = str(input("username: "))
password = str(input("password: "))
f = open("dataBank.txt", 'r')
info = f.read()
if name in info:
return 'Name unavailable'
f.close()
f = open("dataBank.txt", 'w')
info = info + ' ' + name + ' ' + password
f.write(info)
def logIn():
username = str(input("username: "))
password = str(input("password: "))
f = open("dataBank.txt", "r")
info = f.read()
info = info.split()
if name in info:
index = info.index(username)+1
usrPassword = info[index]
if usrPassword == password:
return "welcome back," + username
else:
return 'password incorrect'
else:
return 'Name not found'
print(AskForAccount())
解决方案
我不知道你的逻辑是什么,但要创建一个文件,你需要w+
通过代码尝试一下
def AskForAccount():
status = input("Do you have an account? ")
if status == "yes":
logIn()
elif status == "no":
createAccount()
else:
print("Type yes or no, please.")
AskForAccount()
def createAccount():
name = str(input("username: "))
password = str(input("password: "))
try:
f = open("dataBank.txt", 'r')
info = f.read()
if name in info:
return 'Name unavailable'
f.close()
except:
return 'Data base donest exists. creating one...'
f = open("dataBank.txt", 'w+')
info = info + ' ' + name + ' ' + password
f.write(info)
def logIn():
username = str(input("username: "))
password = str(input("password: "))
f = open("dataBank.txt", "r")
info = f.read()
info = info.split()
if username in info:
index = info.index(username)+1
usrPassword = info[index]
if usrPassword == password:
return "welcome back," + username
else:
return 'password incorrect'
else:
return 'Name not found'
print(AskForAccount())
推荐阅读
- java - ApplicationEventPublisher NullPointerException
- python-3.x - PIP 和 python 安装包
- javascript - 防止通过 Inspector 更改表单/隐藏输入(特别是使用 WordPress)
- azure - 适用于 Web 应用的 Azure ARM 模板未正确配置从中导出的资源
- html - 如何将 excel 中的动态编码超链接转换为等效的 HTML 代码?
- docker - 如何以非交互方式调用 docker-compose 运行命令 - SSH?
- angular - 将 Angular 7 项目转换为 Angular 7 库
- mysql - 是否可以在 mysql 的列中使用数学函数?
- listview - 在 Flutter 的 3000 个项目列表中更改项目的状态
- google-cloud-firestore - Firestore 数据库规则 获取文档