我目前尝试运行以下Python代码:
def from_file(filename, sep='\n'):
"Parse a file into a list of strings, separated by sep."
return (filename).read().strip().split(sep)
我得到和类似的错误: AttributeError:“ str”对象没有属性“ read”
有什么主意吗?
答案 0 :(得分:0)
文件名仅是文件名。您必须打开文件才能创建文件对象。然后您可以从该文件对象读取。
def from_file(filename, sep='\n'):
"""Parse a file into a list of strings, separated by sep"""
with open(filename) as file:
return file.read().strip().split(sep)