Python实现简单的打包
2013-03-14 21:08
274 查看
版权所有,转载请注明出处:http://guangboo.org/2013/03/14/package-with-python-implement
通常我们在对目录进行打包时都是有zip或rar工具,然而现在就遇到一个问题。我可以通过zip来对目录进行打包,但是我没有办法将这样一个文件上传到服务器(内网受限),只能上传图标格式,或文本格式。由于我目录下也都是文本文件,所有这里就想到一个变通的办法,将目录层次结构和文件的内容都存到一个文本文件里面,然后上传。
文本文件的格式也只需要稍作规定,第一行为“打包”目录的根目录,第二行开始为目录名字,文件名,然后是文件内容。格式如下啊:
这样的格式,我们就可以对其解析,并还原到磁盘。那么实现“打包”的过程,使用python来实现,如下:
代码中使用codecs.open方法,主要是可以通过utf-8编码方式来打开文件,并使用os.walk来遍历跟目录src_dir下所有子目录和文件。那么只需要简单调用该方法,传递要“打包”的目录地址,和要“打包”成的文件名即可,如:
这样“打包”过程,便完成了,但是有了打包过程,必定要有“解包”的过程,既然我们知道“打包”后的文件格式,那么也就很容易将文件的内容重新还原到磁盘上,代码如下:
该方法是用正则表达来判断“包”文件的行,是目录还是文件,或者是文件内容。并可以还原目录结构。进行还原时,需要调用decompress方法,传递包文件名称,和存放目录。如:
那么为了方便,我们将两个方法定义到同一文件zip.py中,并添加如下代码,使得可以在命令行中传递参数,实现“打包”和“解包”功能:
那么“打包”和“解包”的过程,就可以通过如下命令来实现:
下载文件:zip.py
通常我们在对目录进行打包时都是有zip或rar工具,然而现在就遇到一个问题。我可以通过zip来对目录进行打包,但是我没有办法将这样一个文件上传到服务器(内网受限),只能上传图标格式,或文本格式。由于我目录下也都是文本文件,所有这里就想到一个变通的办法,将目录层次结构和文件的内容都存到一个文本文件里面,然后上传。
文本文件的格式也只需要稍作规定,第一行为“打包”目录的根目录,第二行开始为目录名字,文件名,然后是文件内容。格式如下啊:
source file root directory >>> [direcotry1] >>> [direcotry2] >>> filename1 file content.................................................. ...................................... >>> filename2 file content.................................................. ......................................
这样的格式,我们就可以对其解析,并还原到磁盘。那么实现“打包”的过程,使用python来实现,如下:
# -*- coding:utf-8 -*- import os import sys import codecs def compress(src_dir, target_file): output = codecs.open(target_file, 'w', 'utf-8') # write root print >> output, src_dir for _dir, sub_dirs, files in os.walk(src_dir): for sub_dir in sub_dirs: print >> output, u'\n>>> [%s%s%s]' % (_dir, os.sep, sub_dir) print >> sys.stdout, u'>>> [%s%s%s]' % (_dir, os.sep, sub_dir) for filename in files: if os.path.splitext(filename)[-1] in ['.txt', '.css', '.js', '.html', '.htm', '.py']: src_file = '%s%s%s' % (_dir, os.sep, filename) print >> sys.stdout, '>>> ', src_file tf = codecs.open(src_file, 'r', 'utf-8') print >> output, u'\n>>> %s' % src_file for line in tf.readlines(): output.write(u'%s' % line) output.close()
代码中使用codecs.open方法,主要是可以通过utf-8编码方式来打开文件,并使用os.walk来遍历跟目录src_dir下所有子目录和文件。那么只需要简单调用该方法,传递要“打包”的目录地址,和要“打包”成的文件名即可,如:
compress('c:\\project', 'c:\\project.txt')
这样“打包”过程,便完成了,但是有了打包过程,必定要有“解包”的过程,既然我们知道“打包”后的文件格式,那么也就很容易将文件的内容重新还原到磁盘上,代码如下:
def decompress(src_file, target_dir): import re input = open(src_file, 'r') root = input.readline() cut_len = len(root) line = input.readline() dir_reg = re.compile('\[(.+)\]') filename_reg = re.compile('^>>> (.+)$') target_file = None while line: if line.startswith('>>> ['): if target_file: target_file.close() target_file = None txt = dir_reg.findall(line) if txt: dir_handler(txt[0][cut_len:], target_dir) elif line.startswith('>>>'): if target_file: target_file.close() target_file = None txt = filename_reg.findall(line) if txt: fn = os.path.join(target_dir, txt[0][cut_len:]) target_file = open(fn, 'w') print >> sys.stdout, 'file:%s' % fn elif target_file: target_file.write(line) line = input.readline() if target_file: target_file.close() input.close() def dir_handler(txt, target_dir): _dir = os.path.join(target_dir, txt) try: os.mkdir(_dir) print >> sys.stdout, 'dir:%s' % _dir except IOError: print '>>> Directory %s created failed.' % _dir
该方法是用正则表达来判断“包”文件的行,是目录还是文件,或者是文件内容。并可以还原目录结构。进行还原时,需要调用decompress方法,传递包文件名称,和存放目录。如:
decompress('c:\\project.txt', 'c:\\project2', )
那么为了方便,我们将两个方法定义到同一文件zip.py中,并添加如下代码,使得可以在命令行中传递参数,实现“打包”和“解包”功能:
if __name__ == '__main__': cmd = sys.argv[1] src = sys.argv[2] target = sys.argv[3] if cmd == 'compress': compress(src, target) elif cmd == 'decompress': decompress(src, target)
那么“打包”和“解包”的过程,就可以通过如下命令来实现:
zip.py compress c:\project c:\package.txt zip.py decompress c:\package.txt c:\project2
下载文件:zip.py
相关文章推荐
- python实现简单抓图并打包成exe
- 用Python实现一个简单的线程池
- 机器学习-线性回归python简单实现
- Python django实现简单的邮件系统发送邮件功能
- 用Python实现简单的HTTP服务器(2)--实现一个基本功能的HTTP服务器
- Python使用tablib生成excel文件的简单实现方法
- Python使用shelve模块实现简单数据存储的方法
- 【python学习笔记】flask实现简单的接收json返回json的接口
- python实现简单爬虫功能
- 详解用python实现简单的遗传算法
- Python简单实现查找一个字符串中最长不重复子串的方法
- 利用字典实现Python中简单的ORM映射
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
- Python(27)使用python的http,cgi模块实现一个简单的web服务
- 【龙书笔记】用Python实现一个简单数学表达式从中缀到后缀语法的翻译器(采用递归下降分析法)
- Python实现简单的四则运算计算器
- 利用Python实现简单猫鼠游戏
- isomap算法 简单python实现
- 如何简单实现接口自动化测试(基于 python)
- Python实现简单截取中文字符串的方法