如何打开函数param提供的文件(函数内部)

2024-04-24 00:00:58 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在学习python3,打开作为函数参数提供的文件时遇到问题。你知道吗

这是我的密码:

def make_list_from_file(file_name):

    with open(file_name,"r") as provided_file:
        temp_list = [line.strip() for line in provided_file]
    detailed_list = [ item.split("\t") for item in temp_list ]
    return detailed_list

make_list_from_file(game_stat.txt)

这给了我:

NameError: name 'Game_Stat' is not defined. 

文件在同一目录中。如果有任何帮助,我将不胜感激。你知道吗


Tags: 文件nameinfromformakeline函数参数
2条回答

更改此行:

with open(file_name,"r") as provided_file:

收件人:

with open(file_name + '.txt',"r") as provided_file:
make_list_from_file(game_stat.txt)

字符串应该用单引号(')或双引号(“)括起来。你知道吗

所以:

make_list_from_file("game_stat.txt")

相关问题 更多 >