python file operate

 同学之间常常要共享精彩电影的,一般电影的大小都是300多M,我有个128M的小MP3,总向同学借大U盘也不是那么回事,有时就得耐着性子用winrar分割压缩,传到自己电脑里再解压缩合并成一个电影文件。用的次数多了大家就都会知道winrar对音乐和电影这些多媒体格式的文件压缩率基本为0,也就是说,即使在那浪费时间浪费计算机资源(非常浪费CPU)最后的结果也只是完成了分割与合并文件的作用,大量的计算做的都是无用功。
  用Pythong写了个简单的文件分割与合并的程序,这样自己的小容量MP3用起来就方便了,而且因为不需要压缩与解压缩的过程,可以大量节省我的耐心。

  
file.py


01import sys
02from os.path import exists
03fileCount = 0
04  
05def splitFile(fileName, partSize=1):
06    # 1024 * 1024 = 1048576
07    length = partSize * 1048576
08    f1 = open(fileName, "rb")
09    while True:
10        content = f1.read(length)
11        if content == "":
12            break
13        newFile = distFile(fileName)
14        f2 = open(newFile, "wb")
15        f2.write(content)
16        f2.close()
17    f1.close()
18    print 'split file complete!'
19      
20  
21  
22def distFile(sourceFile):
23    global fileCount
24    fileCount = fileCount + 1
25    extPos = sourceFile.rfind('.')
26    if extPos > 0:
27        return sourceFile + '.part' + str(fileCount)
28    else:    # extPos == -1
29        print 'File type? Can not split!'
30        sys.exit(1)
31  
32def combine(filename):
33    count = 0
34    extPos = filename.find('.part')
35    if extPos > 0:
36        file = filename[:extPos]
37        f1 = open(file, "wb")
38        while True:
39            count = count + 1
40            partFile = file + '.part' + str(count)
41            if not exists(partFile):
42                break
43            else:
44                f2 = open(partFile, "rb")
45                content = f2.read()
46                f2.close()
47                f1.write(content)
48        f1.close()
49        print 'combine file complete!'
50  
51    else:
52        print 'File type? Can not combine!'
53  
54def usage():
55    print '''usage is "file.py s[c] filename" or "file.py s[c] filename size(M) /n 's' -- 'split', 'c' -- 'combine" '''
56  
57  
58if __name__ == "__main__":
59    if len(sys.argv) !=3 and len(sys.argv) !=4:
60        usage()
61        sys.exit(1)
62    if sys.argv[1] == 's':
63        if len(sys.argv) == 3:
64            splitFile(sys.argv[2])
65        elif len(sys.argv) == 4 and int(sys.argv[3]) > 0:
66            splitFile(sys.argv[2], int(sys.argv[3]))
67        else:
68            usage()
69        sys.exit(1)
70      
71    elif sys.argv[1] =='c':
72        if len(sys.argv) == 3:
73            combine(sys.argv[2])
74        else:
75            usage()
76    else:
77        usage()

  使用方法:
  分割:file.py s 文件路径 每块的文件大小(不填默认为1M)

  合并:file.py c 任意部分文件路径
  因为用winrar解压缩时双击任何一个部分压缩文件都会成功解压该完成文件而不必只能点击part1,所以这个程序也写成给出任何一个部分文件名都可以合并处完整的文件,这样就可以方便的在命令行随便用鼠标拖入一个分割后的部分文件就可以了。估计winrar的.rar 格式文件里面是包含了压缩信息和共有多少个部分文件的,我的这种自己用的小程序就得人工判断分割成了几部分然后全部copy了。

  再用py2exe转换成.exe文件装入U盘就可以了,以后到谁的机器上都可以用了。

转自: http://lulinbest.blog.sohu.com/62764871.html

 

 

用Python进行文件操作是比较简单的,在Python中file是内置类型之一,内置的函数open、file都可以创建file对象,创建好之后就可以对其进行读写等操作。

近几天看Python Programing 3rd ,觉得书很不错。

文件分割的原理很简单:以二进制形式打开文件流,按照指定的大小读取,然后写入新文件。

文件合并的原理正好相反。

下面的代码摘自PP3rd里面。

split_file.py

 

 

01#!/usr/bin/python
02##########################################################################
03# split a file into a set of parts; join.py puts them back together;
04# this is a customizable version of the standard unix split command-line 
05# utility; because it is written in Python, it also works on Windows and
06# can be easily modified; because it exports a function, its logic can 
07# also be imported and reused in other applications;
08##########################################################################
09       
10import sys, os
11kilobytes = 1024
12megabytes = kilobytes * 1000
13chunksize = int(1.4 * megabytes)                   # default: roughly a floppy
14       
15def split(fromfile, todir, chunksize=chunksize): 
16    if not os.path.exists(todir):                  # caller handles errors
17        os.mkdir(todir)                            # make dir, read/write parts
18    else:
19        for fname in os.listdir(todir):            # delete any existing files
20            os.remove(os.path.join(todir, fname)) 
21    partnum = 0
22    input = open(fromfile, 'rb')                   # use binary mode on Windows
23    while 1:                                       # eof=empty string from read
24        chunk = input.read(chunksize)              # get next part <= chunksize
25        if not chunk: break
26        partnum  = partnum+1
27        filename = os.path.join(todir, ('part%04d' % partnum))
28        fileobj  = open(filename, 'wb')
29        fileobj.write(chunk)
30        fileobj.close()                            # or simply open().write()
31    input.close()
32    assert partnum <= 9999                         # join sort fails if 5 digits
33    return partnum
34              
35if __name__ == '__main__':
36    if len(sys.argv) == 2 and sys.argv[1] == '-help':
37        print 'Use: split.py [file-to-split target-dir [chunksize]]'
38    else:
39        if len(sys.argv) < 3:
40            interactive = 1
41            fromfile = raw_input('File to be split? ')       # input if clicked 
42            todir    = raw_input('Directory to store part files? ')
43        else:
44            interactive = 0
45            fromfile, todir = sys.argv[1:3]                  # args in cmdline
46            if len(sys.argv) == 4: chunksize = int(sys.argv[3])
47        absfrom, absto = map(os.path.abspath, [fromfile, todir])
48        print 'Splitting', absfrom, 'to', absto, 'by', chunksize
49       
50        try:
51            parts = split(fromfile, todir, chunksize)
52        except:
53            print 'Error during split:'
54            print sys.exc_info()[0], sys.exc_info()[1]
55        else:
56            print 'Split finished:', parts, 'parts are in', absto
57        if interactive: raw_input('Press Enter key') # pause if clicked

join_file.py

01#!/usr/bin/python
02##########################################################################
03# join all part files in a dir created by split.py, to recreate file.  
04# This is roughly like a 'cat fromdir/* > tofile' command on unix, but is 
05# more portable and configurable, and exports the join operation as a 
06# reusable function.  Relies on sort order of file names: must be same 
07# length.  Could extend split/join to popup Tkinter file selectors.
08##########################################################################
09       
10import os, sys
11readsize = 1024
12       
13def join(fromdir, tofile):
14    output = open(tofile, 'wb')
15    parts  = os.listdir(fromdir)
16    parts.sort()
17    for filename in parts:
18        filepath = os.path.join(fromdir, filename)
19        fileobj  = open(filepath, 'rb')
20        while 1:
21            filebytes = fileobj.read(readsize)
22            if not filebytes: break
23            output.write(filebytes)
24        fileobj.close()
25    output.close()
26       
27if __name__ == '__main__':
28    if len(sys.argv) == 2 and sys.argv[1] == '-help':
29        print 'Use: join.py [from-dir-name to-file-name]'
30    else:
31        if len(sys.argv) != 3:
32            interactive = 1
33            fromdir = raw_input('Directory containing part files? ')
34            tofile  = raw_input('Name of file to be recreated? ')
35        else:
36            interactive = 0
37            fromdir, tofile = sys.argv[1:]
38        absfrom, absto = map(os.path.abspath, [fromdir, tofile])
39        print 'Joining', absfrom, 'to make', absto
40       
41        try:
42            join(fromdir, tofile)
43        except:
44            print 'Error joining files:'
45            print sys.exc_info()[0], sys.exc_info()[1]
46        else:
47           print 'Join complete: see', absto
48        if interactive: raw_input('Press Enter key') # pause if clicked

以上代码在window下面测试成功,可以分割文件文件已经任何二进制文件。

转自:   http://blog.csdn.net/foyuan/archive/2007/08/01/1720600.aspx

--End--

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值