python将文件读取为字符串

时间:2025-04-11 21:48:33

python将文件读取为字符串

参考这个

/nanjunxiao/article/details/9086079

在文件内容不是很大的情况下,可以直接读取

import os

def read_file_as_str(file_path):
    # 判断路径文件存在
    if not (file_path):
        raise TypeError(file_path + " does not exist")

    all_the_text = open(file_path).read()
    # print type(all_the_text)
    return all_the_text