读取文本文件。 参数: - file_path (str): 文件路径。 - chunk_size (int): 当文件大于此大小时,使用多线程读取。 - encoding (str): 文件编码格式,默认为 'utf-8'。 返回: - str: 文件内容。
(file_path, chunk_size, encoding='utf-8')
| 97 | |
| 98 | # 读取文件函数 |
| 99 | def read_txt(file_path, chunk_size, encoding='utf-8'): |
| 100 | """ |
| 101 | 读取文本文件。 |
| 102 | |
| 103 | 参数: |
| 104 | - file_path (str): 文件路径。 |
| 105 | - chunk_size (int): 当文件大于此大小时,使用多线程读取。 |
| 106 | - encoding (str): 文件编码格式,默认为 'utf-8'。 |
| 107 | |
| 108 | 返回: |
| 109 | - str: 文件内容。 |
| 110 | """ |
| 111 | file_size = os.path.getsize(file_path) |
| 112 | if file_size > chunk_size: |
| 113 | logger.info(f"文件 {file_path} 较大,共{file_size} 字节,使用多线程读取") |
| 114 | return read_txt_multithread(file_path, chunk_size, encoding) |
| 115 | else: |
| 116 | logger.info(f"读取文本文件: {file_path}") |
| 117 | with open(file_path, 'r', encoding=encoding) as f: |
| 118 | return f.read() |
| 119 | |
| 120 | |
| 121 | def read_txt_multithread(file_path, chunk_size, encoding='utf-8'): |
no test coverage detected