【python – 如何压缩这个函数?】教程文章相关的互联网学习教程文章

使用Python读写及压缩和解压缩文件的示例

读写文件首先看一个例子:f = open(thefile.txt,w) #以写方式打开, try:f.write(wokao)finally:f.close()文件的打开方式:f = open(‘文件,mode) ‘r:只读(缺省。如果文件不存在,则抛出错误) ‘w:只写(如果文件不存在,则自动创建文件),此时无法调用f.read()方法,且当调用f.write()时,将清空文件原有内容 ‘a:附加到文件末尾 ‘r+:读写如果需要以二进制方式打开文件,需要在mode后面加上字符”b”,比如”rb”,”wb...

windows系统中python使用rar命令压缩多个文件夹示例

代码如下:#!/usr/bin/env python# Filename: backup_ver1.py import osimport time # 1. The files and directories to be backed up are specified in a list.#source=[/home/swaroop/byte,/home/swaroop/bin]source=[D:\\FileCopier\\*.*,D:\\jeecms_doc\\*.*]# If you are using Windows, use source=[rC:\Documents,rD:\Work] or something like that # 2. The backup must be stored in a main backup directory#target_dir=/...

python实现在pickling的时候压缩的方法

本文实例讲述了python实现在pickling的时候压缩的方法。分享给大家供大家参考。 具体方法如下:import cPickle,gzip def save(filename,*objects):fil1 = gzip.open(filename,wb)for obj in objects:cPickle.dump(obj,fil1,protocol = 2)fil1.close() def load(filename):fil1 = gzip.open(filename,rb)while True:try:yield cPickle.load(fil1)except EOFError:breakfil1.close()data1 = [abc,12,23] #几个测试数据 data2 = {1:a...

python通过zlib实现压缩与解压字符串的方法

本文实例讲述了python通过zlib实现压缩与解压字符串的方法。分享给大家供大家参考。具体实现方法如下: 使用zlib.compress可以压缩字符串。使用zlib.decompress可以解压字符串。如下代码如下:#coding=utf-8 import zlib s = "hello word, 00000000000000000000000000000000" print len(s) c = zlib.compress(s) print len(c) d = zlib.decompress(c) print d 示范代码2:代码如下:import zlib message = witch which has which wit...

Python压缩和解压缩zip文件

zip文件是我们经常使用的打包格式之一,python解压和压缩zip效率非凡。 python解压zip文档:代码如下: #/usr/bin/python #coding=utf-8 import os,sys,time import zipfile filename = callofdutyblackopszombies_1349649132343_my.zip #要解压的文件 filedir = data/ #解压后放入的目录 r = zipfile.is_zipfile(filename) if r:starttime = time.time()fz = zipfile.ZipFile(filename,r)for file in fz.namelist():print(file) ...

Python中使用tarfile压缩、解压tar归档文件示例

Python自带的tarfile模块可以方便读取tar归档文件,牛b的是可以处理使用gzip和bz2压缩归档文件tar.gz和tar.bz2。 与tarfile对应的是zipfile模块,zipfile是处理zip压缩的。请注意:os.system(cmd)可以使Python脚本执行命令,当然包括:tar -czf *.tar.gz *,tar -xzf *.tar.gz,unzip等,当我觉得这样尽管可以解决问题,但我觉得很业余。 使用tarfile压缩代码如下: import tarfile #创建压缩包名 tar = tarfile.open("/tmp/tartes...

Python中使用gzip模块压缩文件的简单教程

压缩数据创建gzip文件 先看一个略麻烦的做法import StringIO,gzip content = Life is short.I use python zbuf = StringIO.StringIO() zfile = gzip.GzipFile(mode=wb, compresslevel=9, fileobj=zbuf) zfile.write(content) zfile.close() 但其实有个快捷的封装,不用用到StringIO模块f = gzip.open(file.gz, wb) f.write(content) f.close() 压缩已经存在的文件 python2.7后,可以用with语句import gzip with open("/path/to/fil...

使用Python压缩和解压缩zip文件的教程

python 的 zipfile 提供了非常便捷的方法来压缩和解压 zip 文件。 例如,在py脚本所在目录中,有如下文件:代码如下:readability/readability.js readability/readability.txt readability/readability-print.css readability/sprite-readability.png readability/readability.css 将 readability 目录中的文件压缩到脚本所在目录的 readability.zip 文件中,保持相同的文件结构,然后打印出生成的压缩包 的文件列表,再用两种方式...

Python压缩解压缩zip文件及破解zip文件密码的方法【图】

python 的 zipfile 提供了非常便捷的方法来压缩和解压 zip 文件。 例如,在py脚本所在目录中,有如下文件:readability/readability.js readability/readability.txt readability/readability-print.css readability/sprite-readability.png readability/readability.css 将 readability 目录中的文件压缩到脚本所在目录的 readability.zip 文件中,保持相同的文件结构,然后打印出生成的压缩包 的文件列表,再用两种方式分别解压文...

压缩包密码破解示例分享(类似典破解)

昨天翻硬盘,找到一个好东西,可惜自己加了密码自己不记得了。试了几个常用的没试出来,于是写了这么个小脚本来替我尝试。。呵呵,还真给解出来了。python脚本内容如下,跑跑自己加密的压缩包还不错代码如下:# -*- coding: utf-8 -*-import sys,osdef IsElementUniq(list): """ 判断list中的元素是否为唯一的 """ for word in list: if list.count(word)>1: return False return Truedef G...

pythonzip文件压缩

从简单的角度来看的话,zip格式会是个不错的选择,而且python对zip格式的支持够简单,够好用。1)简单应用 如果你仅仅是希望用python来做压缩和解压缩,那么就不用去翻文档了,这里提供一个简单的用法,让你一看就能明白。 import zipfile f = zipfile.ZipFile('filename.zip', 'w' ,zipfile.ZIP_DEFLATED) f.write('file1.txt') f.write('file2.doc') f.write('file3.rar') f.close() f.zipfile.ZipFile('filename') f.extractall(...

在Python中使用pngquant压缩png图片的教程

说到png图片压缩,可能很多人知道TinyPNG这个网站。但PS插件要钱(虽然有破解的),Developer API要连到他服务器去,不提网络传输速度,Key也是有每月限制的。但是貌似tinyPNG是使用了来自于 pngquant 的技术,至少在 http://pngquant.org/ 中是如此声称的:TinyPNG and Kraken.io — on-line interfaces for pngquant。如果真是这样,我很想对TinyPNG说呵呵。后者是开源的,连首页中提供的GUI工具也都是开源的。并且TinyPNG在首页...

Python格式化压缩后的JS文件的方法

本文实例讲述了Python格式化压缩后的JS文件的方法。分享给大家供大家参考。具体分析如下: 该脚本可以把压缩后的js文件格式上进行些还原,当然不会百分百完美,暂不处理语法问题,只是为了方便阅读js代码lines = open("unformated.js").readlines()[0].split(";") #一般压缩后的文件所有代码都在一行里 #视情况设定索引,我的情况时第0行是源代码。 indent = 0 formatted = [] for line in lines:newline = []for char in line:new...

python自动zip压缩目录的方法

本文实例讲述了python自动zip压缩目录的方法。分享给大家供大家参考。具体实现方法如下: 这段代码来压缩数据库备份文件,没有使用python内置的zip模块,而是使用了zip.exe文件# Hello, this script is written in Python - http://www.python.org # # autozip.py 1.0p # # This script will scan a directory (and its subdirectories) # and automatically zip files (according to their extensions). # # This script does not...

在Python中使用zlib模块进行数据压缩的教程

Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等。上次介绍了zipfile模块,今天就来讲讲zlib模块。 zlib.compress(string[, level]) zlib.decompress(string[, wbits[, bufsize]]) zlib.compress用于压缩流数据。参数string指定了要压缩的数据流,参数level指定了压缩的级别,它的取值范围是1到9。压缩速度与压缩率成反比,1表示压缩速度最快,而压缩率最低,而9则表示压缩速度最慢但压缩率最高。z...