惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - Mojies

Prompt 相关 T-Lite 简介 CRC 计算 C 语言例子 UNIX 环境编程 Note ( UPDATING ) 航模.fs.ia6b 接收机记录 线程同步 STM32 Note Linux FrameBuffer note VIM Note Android 开发笔记(更新中....) Effective C++ (暂停更新) Linux 命令行对比二进制文件脚本 Linux Note (Updateing) 关于 HID 你可能需要知道的几件事 (更新中...) 自制树莓派 3D 模型,附 STL 文件 《重构改善既有代码的设计》Tips 翻译: buildroot 用户手册 (更新中...) Some view of engineers 地球地址
一个 python 拆解文本文件的工具
Mojies · 2024-01-10 · via 博客园 - Mojies

背景

你是否有遇到过文本文档太大无法打开的情况?比如说压测了好几天,生成了一个十几 G 的日志文件。

下面这个脚本可以帮助你将一个大文件分解成一个小文件。

假设文件名位:splitfile.py

使用方法位:python splitfile.py log 20

该文件将会将 log 文件拆分成 log.0 log.1 log.2 ... log.19 等 20 个文件

源码

import os
import sys

if len( sys.argv ) < 3:
    print( "Please specify file and how many you want split" );
    print( "python %s log 10"%(sys.argv[0]) );
    sys.exit(-1)

logfile = sys.argv[1]
cutnbs = int(sys.argv[2])

if os.path.isfile( logfile ) == False:
    print( "%s is not file"%( logfile ) );
    exit(-1)

f_stat = os.stat( logfile )
print(f_stat)

f_basename = os.path.basename( logfile )
print(f_basename)

f_size = f_stat.st_size
sub_f_size = f_size / cutnbs + cutnbs

# print( f_size )

infd = open( logfile, 'rb' );

for i in range( cutnbs ):
    size_count  = 0
    o_filename = "./%s.%d"%( f_basename, i )
    print( "out filename: %s"%( o_filename ) )
    print( "start dumpL %s"%( o_filename ) )

    if os.path.isfile( o_filename ):
        os.remove( o_filename )
    o_fd = open( o_filename, 'xb' )

    while size_count < sub_f_size:
        line = infd.read( 1024*1024 )
        if len(line) == 0:
            break
        o_fd.write( line )
        size_count += len( line )

    o_fd.close()


infd.close()
exit(0)