【发布时间】:2019-09-27 07:31:57
【问题描述】:
我尝试定义一个函数,它将获取文件路径并将其转换为字符串。 这是我想出的辩护:
def get_book(file_path):
'''Takes a file path and returns the entire book as a string.'''
with open(file_path, 'r', 'utf-8') as infile:
content = infile.read()
return content
AnnaKarenina = get_book('../Python/Data/books/AnnaKarenina.txt')
我现在得到 TypeError: an integer is required (got type str)
我也尝试使用 os.path、不同种类的斜线和其他技巧来使用 windows 打开文件,但都返回找不到文件的错误。
有谁知道我做错了什么?
【问题讨论】: