python-3.x - 在python3.x中将大文件拆分为多个文件
问题描述
如果 file_write 的文件大小大于 20MB,我想将文件拆分为多个文件。在 Random 函数中,我打开 big_file.txt 并使用 remove_noise() 去除噪声并将干净的行写入 outfile。我不确定如何根据当前实现中的大小拆分文件。请找到下面的代码:(抱歉没有提供正确的实现示例,因为它真的很复杂)我已经通过这个链接的例子:Split large text file(about 50GB) into multiple files
import os
def parses(lines, my_date_list):
for line in reversed(list(lines)):
line = line.strip()
if not line:
continue
date_string = "2019-11-01" # assumption
yield date_string, line
def remove_noise(line):
""" dummy function"""
return line
def random_function(path, output, cutoff="2019-10-31"):
my_date_list = []
if os.path.exists(path):
with open(path) as f:
lines = parses(f, my_date_list)
for date, line in lines:
if cutoff <= date:
results = remove_noise(line)
output.write(results + '\n')
continue
else:
break
在向 写入行时output
,我需要检查大小。如果大小达到 20MB,我想把它写到第二个 {may be output_2
} 等等。
if __name__ == '__main__':
path = "./big_file.txt"
file_write = "./write_file.txt"
with open(file_write) as outfile:
random_function(path=path, output=outfile)
解决方案
推荐阅读
- angular - 主题不返回任何东西
- c# - asp.net mvc中的模型推荐系统
- python - Python PIP 安装 v3.8
- bounding-box - Revit 房间边界框和标高偏移
- mongodb - 禁用 Go mongo bson 地图中的某些字段
- c - 从 C 中,我如何打印 Lua 堆栈的内容?
- mysql - 如何使用 Excel 查看 MySQL 中的数据?
- javascript - 如何创建与输入文本匹配的搜索栏并仅显示属性中包含该文本的图像(href)???jQuery
- javascript - 用地图在反应组件内打印数字?
- javascript - Javascript 不会在我的 ruby on rails 应用程序中运行,但会在单独的 html/css/js 文件中运行