根据文件扩展名读取文件内容,并自动判断是否使用多线程。 TODO: 修正多线程读取功能,暂不可用,所以阈值设置的很大 参数: - file_path (str): 文件路径。 - chunk_size (int): 当文件大于此大小时,使用多线程读取,单位为字节,默认为 10000MB。 - line_threshold (int): 当文件行数大于此值时,使用多线程读取,默认为 1000000 行。 - encoding (str): 文件编码格式,默认为 'utf-8'。 返回: - list 或 str: 读取
(file_path, chunk_size=1024*1024*10000, line_threshold=1000000, encoding='utf-8')
| 18 | |
| 19 | |
| 20 | def read(file_path, chunk_size=1024*1024*10000, line_threshold=1000000, encoding='utf-8'): |
| 21 | """ |
| 22 | 根据文件扩展名读取文件内容,并自动判断是否使用多线程。 |
| 23 | TODO: 修正多线程读取功能,暂不可用,所以阈值设置的很大 |
| 24 | |
| 25 | 参数: |
| 26 | - file_path (str): 文件路径。 |
| 27 | - chunk_size (int): 当文件大于此大小时,使用多线程读取,单位为字节,默认为 10000MB。 |
| 28 | - line_threshold (int): 当文件行数大于此值时,使用多线程读取,默认为 1000000 行。 |
| 29 | - encoding (str): 文件编码格式,默认为 'utf-8'。 |
| 30 | |
| 31 | 返回: |
| 32 | - list 或 str: 读取的文件内容。 |
| 33 | """ |
| 34 | file_ext = os.path.splitext(file_path)[1].lower() |
| 35 | |
| 36 | try: |
| 37 | if file_ext == '.txt': |
| 38 | return read_txt(file_path, chunk_size, encoding) |
| 39 | elif file_ext == '.csv': |
| 40 | return read_csv(file_path, chunk_size, encoding) |
| 41 | elif file_ext == '.json': |
| 42 | return read_json(file_path, encoding) |
| 43 | elif file_ext == '.yaml' or file_ext == '.yml': |
| 44 | return read_yaml(file_path, encoding) |
| 45 | elif file_ext == '.xlsx': |
| 46 | return read_xlsx(file_path, encoding) |
| 47 | elif file_ext == '.md': |
| 48 | return read_markdown(file_path, encoding) |
| 49 | elif file_ext == '.jsonl': |
| 50 | return read_jsonl(file_path, chunk_size, line_threshold, encoding) |
| 51 | else: |
| 52 | raise ValueError(f"不支持的文件格式: {file_ext}") |
| 53 | except Exception as e: |
| 54 | logger.error(f"读取文件 {file_path} 时出错: {e}") |
| 55 | raise |
| 56 | |
| 57 | |
| 58 | def write(file_path, data, mode='a', chunk_size=1024*1024*100000, encoding='utf-8'): |
nothing calls this directly
no test coverage detected