Python 按行切割文件

在遇到各种比较大的问题时候,行数过多导致打开处理会出问题,则需要切割,则可以用按行数切割的方法来处理。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python  
#--*-- coding:utf-8 --*--

import os

class SplitFiles():
"""按行分割文件"""

def __init__(self, file_name, line_count=200):
"""初始化要分割的源文件名和分割后的文件行数"""
self.file_name = file_name
self.line_count = line_count

def split_file(self):
if self.file_name and os.path.exists(self.file_name):
try:
with open(self.file_name) as f : # 使用with读文件
temp_count = 0
temp_content = []
part_num = 1
for line in f:
if temp_count < self.line_count:
temp_count += 1
else :
self.write_file(part_num, temp_content)
part_num += 1
temp_count = 1
temp_content = []
temp_content.append(line)
else : # 正常结束循环后将剩余的内容写入新文件中
self.write_file(part_num, temp_content)

except IOError as err:
print(err)
else:
print("%s is not a validate file" % self.file_name)

def get_part_file_name(self, part_num):
""""获取分割后的文件名称:在源文件相同目录下建立临时文件夹temp_part_file,然后将分割后的文件放到该路径下"""
temp_path = os.path.dirname(self.file_name) # 获取文件的路径(不含文件名)
part_file_name = temp_path + "temp_part_file"
if not os.path.exists(temp_path) : # 如果临时目录不存在则创建
os.makedirs(temp_path)
part_file_name += os.sep + "temp_file_" + str(part_num) + ".part"
return part_file_name

def write_file(self, part_num, *line_content):
"""将按行分割后的内容写入相应的分割文件中"""
part_file_name = self.get_part_file_name(part_num)
print(line_content)
try :
with open(part_file_name, "w") as part_file:
part_file.writelines(line_content[0])
except IOError as err:
print(err)

if __name__ == "__main__":
sf = SplitFiles(r"F:\multiple_thread_read_file.txt")
sf.split_file()

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×