Python实现简单的打包
来源:互联网 发布:好的数据圈网站 编辑:程序博客网 时间:2024/06/04 18:48
版权所有,转载请注明出处:http://guangboo.org/2013/03/14/package-with-python-implement
通常我们在对目录进行打包时都是有zip或rar工具,然而现在就遇到一个问题。我可以通过zip来对目录进行打包,但是我没有办法将这样一个文件上传到服务器(内网受限),只能上传图标格式,或文本格式。由于我目录下也都是文本文件,所有这里就想到一个变通的办法,将目录层次结构和文件的内容都存到一个文本文件里面,然后上传。
文本文件的格式也只需要稍作规定,第一行为“打包”目录的根目录,第二行开始为目录名字,文件名,然后是文件内容。格式如下啊:
source file root directory>>> [direcotry1]>>> [direcotry2]>>> filename1file content........................................................................................>>> filename2file content........................................................................................
这样的格式,我们就可以对其解析,并还原到磁盘。那么实现“打包”的过程,使用python来实现,如下:
# -*- coding:utf-8 -*-import osimport sysimport codecsdef 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.txtzip.py decompress c:\package.txt c:\project2
下载文件:zip.py
- Python实现简单的打包
- python打包为exe的简单操作
- python 简单打包
- python实现简单抓图并打包成exe
- python 排序的简单实现
- Python 实现简单的通讯录
- Python实现的简单计算器
- 实现的简单python例子
- Python 实现简单的GUI
- Python 实现简单的迭代器
- Python 实现简单的数组
- Python 实现简单的矩阵
- Python实现简单的爬虫
- Python实现简单的爬虫
- Python实现简单的爬虫
- python 分布式的简单实现
- Python简单爬虫的实现
- Python实现简单的API
- Java中 jdbc 调用存储过程 用hibernate连接数据库
- java的内省
- 作业6-3
- java字符串取千分位数。
- window 下的socket编程!!
- Python实现简单的打包
- 虚拟文件系统
- C/C++ 位域之二
- 如何在两个ViewController之间传递数据
- Integer.parseInt()和Integer.valueOf()有什么区别
- Windows下用RawCap抓包localhost 127.0.0.1,WireShark不行
- 圆周率之随笔(3)
- lisp(一)环境搭建
- 设计模式 - 单例模式