| 196 | print(f"处理文件时出现错误:{e}") |
| 197 | |
| 198 | def file_split(input_file, num): |
| 199 | # 假设你的文件名是 'data.txt' |
| 200 | # input_file = 'input_file.txt' |
| 201 | input_name = input_file.split('.')[0] |
| 202 | file_type = input_file.split('.')[1] |
| 203 | output_files = [] |
| 204 | for i in range(num): |
| 205 | output_files.append(f'{input_name}_part{i}.{file_type}') |
| 206 | |
| 207 | # 计算文件总行数 |
| 208 | with open(input_file, 'r', encoding='utf-8') as file: |
| 209 | total_lines = sum(1 for _ in file) |
| 210 | |
| 211 | # 每个部分的行数 |
| 212 | lines_per_part = total_lines // 4 |
| 213 | |
| 214 | # 逐行读取并写入 |
| 215 | with open(input_file, 'r', encoding='utf-8') as file: |
| 216 | current_part = 0 |
| 217 | current_line = 0 |
| 218 | output_file = open(output_files[current_part], 'w', encoding='utf-8') |
| 219 | |
| 220 | for line in file: |
| 221 | output_file.write(line) |
| 222 | current_line += 1 |
| 223 | |
| 224 | # 检查是否需要切换到下一个文件 |
| 225 | if current_line >= lines_per_part and current_part < 3: |
| 226 | output_file.close() |
| 227 | current_part += 1 |
| 228 | output_file = open(output_files[current_part], 'w', encoding='utf-8') |
| 229 | current_line = 0 |
| 230 | |
| 231 | # 关闭最后一个文件 |
| 232 | output_file.close() |
| 233 | |
| 234 | def print_file(infile, n=10): |
| 235 | # 打开文件并读取前10行 |