Skip to main content
 首页 » 编程设计

python之将 Python v2 函数移植到 v3

2025年05月04日13artech

此 v2 函数在 v3 中运行时给出“NameError:全局名称‘file’未定义”错误。

def from_file(filename, sep='\n'):   
    "Parse a file into a list of strings, separated by sep."   
    return file(filename).read().strip().split(sep)   

有人可以提供 v3 版本吗?

请您参考如下方法:

将 file 调用更改为 open。