[英]FileNotFoundError: No such file or directory ERROR
football_players = []
while True:
print("""
*******************
CHOOSE OPERATION:
1. ADD FOOTBALLER (NAME SURNAME, FOOTBALL TEAM)
2. SHOW ME PLAYERS OF FENERBAHÇE TEAM
3. SHOW ME PLAYERS OF GALATASARAY TEAM
ENTER 'q' to quit...
*******************
""")
operation = input("Operation:")
if (operation == "q"):
break
elif (operation == "1"):
player = list()
players_numbers = int(input("Kaç adet futbolcu ekleyeceksiniz?"))
for i in range(players_numbers):
player.append(input("Name Surname, Team:").split(","))
with open("players.txt", "w", encoding = "utf-8") as file:
for i in player:
file.write("Name Surname:{} Team:{}\n".format(i[0], i[1]))
if (i[1] == "Fenerbahçe"):
with open("fenerbahçe_players.txt", "a", encoding = "utf-8") as file2:
file2.write("Name Surname:{} Team:{}\n".format(i[0], i[1]))
elif (i[1] == "Galatasaray"):
with open("galatasaray_players.txt", "a", encoding = "utf-8") as file3:
file3.write("Name Surname:{} Team:{}\n".format(i[0], i[1]))
elif (operation == "2"):
with open("fenerbahçe_players.txt", "r", encoding = "utf-8") as file2:
file2.readlines()
elif (operation == "3"):
with open("galatasaray_players.txt", "r", encoding = "utf-8") as file3:
file3.readlines()
I get this below error.我收到以下错误。 And, i cant find the solution.
而且,我找不到解决方案。 I need to take player names from user and write these into
players.txt
.我需要从用户那里获取玩家名称并将它们写入到
players.txt
。 After that, I need to write 2 .txt
file for their team.之后,我需要为他们的团队编写 2 个
.txt
文件。 Can you help me, please?你能帮我吗?
> FileNotFoundError: [Errno 2] No such file or directory:
> 'fenerbahçe_players.txt'
You are trying to open a file您正在尝试打开文件
with open("fenerbahçe_players.txt", "r", encoding = "utf-8") as file2:```
But that file does not exist.但是那个文件不存在。
First point: relative path are resolved against the current working directory , not against the directory where your script or module is installed.第一点:相对路径是针对当前工作目录解析的,而不是针对安装脚本或模块的目录。 The only safe solution is to use an absolute path - which can be constructed dynamically, using either the script or module path, some known system-specific location (ie
/home/<username>
on unices etc), or a user setting (using environment variable, a config file or whatever).唯一安全的解决方案是使用绝对路径 - 可以使用脚本或模块路径、一些已知的系统特定位置(即 unices 上的
/home/<username>
等)或用户设置(使用环境变量,配置文件或其他)。
Second point: in all cases, if your user selects options 2 or 3 before he ever added any player (option 1), the team files have not been created yet, so of course they might not exist yet.第二点:在所有情况下,如果您的用户在添加任何球员之前选择了选项 2 或 3(选项 1),则团队文件尚未创建,因此当然它们可能还不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.