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

推荐订阅源

T
The Exploit Database - CXSecurity.com
F
Fortinet All Blogs
U
Unit 42
F
Full Disclosure
雷峰网
雷峰网
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
罗磊的独立博客
D
DataBreaches.Net
C
Check Point Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
O
OpenAI News
C
CXSECURITY Database RSS Feed - CXSecurity.com
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Hacker News
The Hacker News
Webroot Blog
Webroot Blog
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
P
Proofpoint News Feed
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
Google Online Security Blog
Google Online Security Blog
H
Help Net Security
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
M
MIT News - Artificial intelligence
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
MyScale Blog
MyScale Blog
腾讯CDC

博客园 - 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)