view_class = input("What class score would you like to view?")
view_class = str(view_class) + ".txt"
如果我进入" 1" (r任何其他数字),错误是
FileNotFoundError: [Errno 2] No such file or directory: '1.txt'
:(
这是python idle 3.4.2
def alphabetical():
global view_class, name, name_scores, class_number, score
view_class = input("What class score would you like to view?")
view_class = str(view_class) + ".txt"
class_number = view_class
with open(class_number, "r") as file: #1
file.seek() #2
scores = file.readlines() #3
with open(class_number) as file:
for name in sorted(name_scores): #16
print(name, '-', name_scores[name]) #17
alphabetical()
编辑:这是后续代码 我不认为问题出在文件目录中,因为我从其他地方的同一文本文件中检索了数据,并且不需要文件目录
答案 0 :(得分:0)
您还需要提供文件位置。假设您的文件位于D:\ folder1 \ 1.txt
中然后代码应该跟随
str_path = "D:\\folder1\\"
view_class = input("What class score would you like to view?")
view_class = str_path + str(view_class) + ".txt"