Python文件合并与分割操作方法工具,python文件合并,编橙之家Python之家
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
编橙之家文章,
相关内容
- 如何用Python创建生成xml文档文件的方法,pythonxml,用Py
- 用Python解压缩rar、zip文件的方法,python解压缩rarzip,编橙
- 游戏水桶倒水问题Python语言的解决方法,倒水python,Py
- Python算法--最长公共子串算法代码讲解,python算法,Pyt
- 如何用Python os.path.walk方法遍历搜索文件内容的操作详解
- Python脚本随机生成中文验证码源码实例分析,python实例
- Python标准库模块之Sys使用详解,python使用详解,本文主要
- 本地服务更新Python代码 如要使用请适当的修改,pytho
- python抽奖 系统算法代码的简单实现,python抽奖算法代码
- 如何用Python代码实现自动比较两个文件中的代码变化?
评论关闭