Python文件合并与分割操作方法工具,python文件合并,编橙之家Python之家


编橙之家Python之家,之前的文章中有为大家介绍过如何使用python对文件进行分割或是合并的操作方法,今天再次为大家示例了同样功能,但操作方法却不相同。利用Python对PDF文件进行分割也是可以的,合并python按行分割文件可以在原有代码的基础上做改进。

把一个文件分割成一组部件加入.py文件中;这是一个可定制的标准unix命令行,它是用Python编写的它也适用于Windows。

Python文件合并与分割操作方法工具代码

#!/usr/bin/python     import sys, oskilobytes = 1024megabytes = kilobytes * 1000chunksize = int(1.4 * megabytes)                   # default: roughly a floppy     def split(fromfile, todir, chunksize=chunksize):     if not os.path.exists(todir):                  # caller handles errors        os.mkdir(todir)                            # make dir, read/write parts    else:        for fname in os.listdir(todir):            # delete any existing files            os.remove(os.path.join(todir, fname))     partnum = 0    input = open(fromfile, 'rb')                   # use binary mode on Windows    while 1:                                       # eof=empty string from read        chunk = input.read(chunksize)              # get next part <= chunksize        if not chunk: break        partnum  = partnum+1        filename = os.path.join(todir, ('part%04d' % partnum))        fileobj  = open(filename, 'wb')        fileobj.write(chunk)        fileobj.close()                            # or simply open().write()    input.close()    assert partnum <= 9999                         # join sort fails if 5 digits    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? ')       # input if clicked             todir    = raw_input('Directory to store part files? ')        else:            interactive = 0            fromfile, todir = sys.argv[1:3]                  # args in cmdline            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') # pause if clickedjoin_file.py

Python文件合并与分割操作方法,第二部分代码如下:

#!/usr/bin/python#www.iplaypy.com     import os, sysreadsize = 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') # pause if clicked

Python文件合并与分割相关文章推荐:

1、Python合并多个文件为一个文本文件
2、Python读取分割压缩TXT文本文件的方法
3、Python创建生成xml文档文件的方法
4、最简单Python删除目录下文件内容的方法
5、Python open()函数文件打开、读、写write

编橙之家文章,

评论关闭