Skip to main content
 首页 » 编程设计

python之如何打开函数参数提供的文件(内部函数)

2024年09月07日14zdz8207

我目前正在学习 Python 3,我在打开作为函数参数提供的文件时遇到问题。

这是我的代码:

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.  

该文件位于同一目录中。如果有任何帮助,我将不胜感激。

请您参考如下方法:

make_list_from_file(game_stat.txt) 

字符串应该用单引号 (') 或双引号 (") 括起来。

所以:

make_list_from_file("game_stat.txt")