吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8303|回复: 5
收起左侧

[Python 转载] python打造简单文件切割机

 关闭 [复制链接]
missviola 发表于 2010-3-10 15:56
好久不在52发帖了,最近发生了很多事情,一直都没能静下心来学习。。。 这个代码是我以前学习python的时候看到的,感觉很不错,非常小巧,而且功能也很不错,这里就和大家一起分享下吧。。。 话说这里python的文章几乎没有啊 汗汗 - -|||||||

切割文件的代码(split.py):
 
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int(1.4 * megabytes)
def split(fromfile, todir, chunksize=chunksize):
    if not os.path.exists(todir):
        os.mkdir(todir)
    else:
        for fname in os.listdir(todir):
            os.remove(os.path.join(todir,fname))
    partnum = 0
    input = open(fromfile, 'rb')
    while 1:
        chunk = input.read(chunksize)
        if not chunk:break
        partnum = partnum + 1
        filename = os.path.join(todir, ('part%4d' % partnum))
        fileobj = open(filename, 'wb')
        fileobj.write(chunk)
        fileobj.close()
    input.close()
    assert partnum <= 9999
    return partnum
if __name__ == '__main__':
    if len(sys.argv) == 2 and sys.argv[1] == '-help':
        print 'Use: split.py [file-to-split target-dir [chunksize]]'
    else:
        if len(sys.argv) < 3:
            interactive = 1
            fromfile = raw_input('File to be split? ')
            todir = raw_input('Directory to store part files?')
        else:
            interactive = 0
            fromfile, todir = sys.argv[1:3]
            if len(sys.argv) == 4: chunksize = int(sys.argv[3])
        absfrom,absto = map(os.path.abspath, [fromfile,todir])
        print 'Splitting', absfrom, 'to', absto, 'by', chunksize
        try:
            parts = split(fromfile, todir, chunksize)
        except:
            print 'Error during split;'
            print sys.exc_info()[0],sys.exc_info[1]
        else:
            print 'Split finished', parts, 'parts are in', absto
        if interactive: raw_input('Press Enter Key')
            

合并文件的代码(join.py):
 
import os, sys
readsize = 1024
def join(fromdir, tofile):
    output = open(tofile, 'wb')
    parts = os.listdir(fromdir)
    parts.sort()
    for filename in parts:
        filepath = os.path.join(fromdir, filename)
        fileobj = open(filepath, 'rb')
        while 1:
            filebytes = fileobj.read(readsize)
            if not filebytes: break
            output.write(filebytes)
        fileobj.close()
    output.close()
if __name__ == '__main__':
    if len(sys.argv) == 2 and sys.argv[1] == '-help':
        print 'Use: join.py [from-dir-name to-file-name]'
    else:
        if len(sys.argv) != 3:
            interactive = 1
            fromdir = raw_input('Directory containing part files?')
            tofile = raw_input('Name of file to be recreated?')
        else:
            interactive = 0
            fromdir, tofile = sys.argv[1:]
        absfrom, absto = map(os.path.abspath, [fromdir, tofile])
        print 'Joining', absfrom ,'to make', absto
        try:
            join(fromdir, tofile)
        except:
            print 'Error joining files;'
            print sys.exc_info()[0],sys.exc_info()[1]
        else:
            print 'Join complete: see', absto
        if interactive: raw_input('Press Enter key')  
            

在Windows SP3 + python 2.5下编译通过。
用法:

QQ截图未命名.png

源代码:
源代码.zip (1.42 KB, 下载次数: 8)

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

roxiel 发表于 2010-3-10 16:06
想学的东西,仔细数一数,实在有很多了[s:38]

。。。不过还是来支持则个
Hmily 发表于 2010-3-10 16:07
sealzhang 发表于 2010-3-13 08:58
cnpn 发表于 2011-2-8 13:10
正想学习,想找一个读文件的方法,用“。”号分割分段读取文件。
gongsui 发表于 2011-2-8 20:58
不错,支持一下,python就是搭建环境太麻烦了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-18 11:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表