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()
我得到以下错误。而且,我找不到解决方案。我需要从用户那里获取玩家名称,并将其写入players.txt
。之后,我需要为他们的团队编写2个.txt
文件。你能帮我吗?
> FileNotFoundError: [Errno 2] No such file or directory:
> 'fenerbahçe_players.txt'
答案 0 :(得分:0)
您正在尝试打开文件
with open("fenerbahçe_players.txt", "r", encoding = "utf-8") as file2:```
但是该文件不存在。
答案 1 :(得分:0)
第一点:相对路径是根据current working directory而不是针对您的脚本或模块的安装目录来解析的。唯一安全的解决方案是使用绝对路径-可以使用脚本或模块路径,某些已知的系统特定位置(例如,{ice上的/home/<username>
)或用户设置(使用环境)来动态构建绝对路径变量,配置文件或其他任何内容。
第二点:在所有情况下,如果您的用户在添加任何玩家之前选择了选项2或3(选项1),则尚未创建团队文件,因此当然可能还不存在。