写入文本文件。 参数: - file_path (str): 文件路径。 - data (str): 要写入的数据。 - mode (str): 写入模式,默认为 'a'(追加模式),可以选择 'w'(覆盖模式)。 - chunk_size (int): 当数据量较大时,使用多线程写入。 - encoding (str): 写入文件的编码格式,默认为 'utf-8'。
(file_path, data, mode, chunk_size, encoding='utf-8')
| 367 | |
| 368 | # 写入文件函数 |
| 369 | def write_txt(file_path, data, mode, chunk_size, encoding='utf-8'): |
| 370 | """ |
| 371 | 写入文本文件。 |
| 372 | |
| 373 | 参数: |
| 374 | - file_path (str): 文件路径。 |
| 375 | - data (str): 要写入的数据。 |
| 376 | - mode (str): 写入模式,默认为 'a'(追加模式),可以选择 'w'(覆盖模式)。 |
| 377 | - chunk_size (int): 当数据量较大时,使用多线程写入。 |
| 378 | - encoding (str): 写入文件的编码格式,默认为 'utf-8'。 |
| 379 | """ |
| 380 | data_size = len(data) |
| 381 | if data_size > chunk_size: |
| 382 | logger.info(f"数据较大,启用多线程写入") |
| 383 | write_txt_multithread(file_path, data, mode, chunk_size, encoding) |
| 384 | else: |
| 385 | logger.info(f"写入文本文件: {file_path}") |
| 386 | with open(file_path, mode, encoding=encoding) as f: |
| 387 | f.write(data) |
| 388 | |
| 389 | |
| 390 | def write_txt_multithread(file_path, data, mode, chunk_size, encoding='utf-8'): |
no test coverage detected