002_009 Python 从Zip中读取数据 直接检查一个zip格式的归档文件部分或所有文件而且不用解压
2014-03-14 22:08
991 查看
代码如下:
打印结果如下:
中国
File: 2K_XP_COM/
has 0 bytes
File: 2K_XP_COM/usb2ser_2kxp.cat
has 10250 bytes
File: 2K_XP_COM/usb2ser_2kXP.inf
has 3276 bytes
File: 2K_XP_COM/usb2ser_XP64.inf
has 1040 bytes
File: Vista/
has 0 bytes
File: Vista/usb2ser_Vista.inf
has 1083 bytes
File: Vista/usb2ser_Vista64.inf
has 1040 bytes
w:\usertemp\tmpu5ghmk.zip
hello world fromd:\1237.zip\hello.py
['d:\\1237.zip', 'F:\\workspace\\StudyPy\\src\\basic', 'F:\\workspace\\StudyPy\\src', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages', 'C:\\Windows\\system32\\python27.zip']
#encoding=utf-8 print '中国' #直接检查一个zip格式的归档文件部分或所有文件而且不用解压 import zipfile zp = zipfile.ZipFile(r'D:\program files\ROMasterLab\Driver.zip','r') for filename in zp.namelist(): print 'File:',filename bytes = zp.read(filename, None) print 'has',len(bytes),'bytes' #创建ZipFile import zipfile,tempfile,os,sys filename = r'd:\1237.zip' z = zipfile.ZipFile(filename,'w') z.writestr('hello.py', 'def f(): return "hello world from"+__file__\n') z.close() sys.path.insert(0, filename) import hello print hello.f() print sys.path os.unlink(filename)
打印结果如下:
中国
File: 2K_XP_COM/
has 0 bytes
File: 2K_XP_COM/usb2ser_2kxp.cat
has 10250 bytes
File: 2K_XP_COM/usb2ser_2kXP.inf
has 3276 bytes
File: 2K_XP_COM/usb2ser_XP64.inf
has 1040 bytes
File: Vista/
has 0 bytes
File: Vista/usb2ser_Vista.inf
has 1083 bytes
File: Vista/usb2ser_Vista64.inf
has 1040 bytes
w:\usertemp\tmpu5ghmk.zip
hello world fromd:\1237.zip\hello.py
['d:\\1237.zip', 'F:\\workspace\\StudyPy\\src\\basic', 'F:\\workspace\\StudyPy\\src', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages', 'C:\\Windows\\system32\\python27.zip']
相关文章推荐
- Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
- 002_010 Python 处理字符串中的Zip文件 你的程序收到了一个字符串,内容是一个ZIP文件,需要读取zip文件的信息
- Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
- 在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。
- 将文本文件中的数据读取到一个二维数组中,求出这些数据的中位数,然后求所有数据与中位数的差值的绝对值和,并求出和的最小值,最后把程序运行的结果写到一个txt文件中
- 获得(读取)一个文件内的所有数据的字节总数
- python 从两个配置文件读取数据,形成一个url思路代码
- Python读取某个目录下的zip压缩包解压开后计算每个小文件的md5值,并将压缩包名字、里面小文件名字、以及对应的md5值写入csv文件
- 【python】如何批量读取文件夹的所有文件数据,os模块与open函数结合使用实例
- python 读取一个目录下的所有目录和文件
- Python读取一个目录下所有目录和文件的方法
- Python:一个类,读取邮件文件,然后返回其指定的部分,例如Subject, Date, Body, attachementsname or attachement.
- C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来
- Python读取文件中一串字符串并且把其中所有的数字组成一个新的字符串,并且打印出来
- C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来
- python 读取一个目录下的所有目录和文件
- Python读取一个目录下所有目录和文件的方法
- python直接按行读取gz压缩文件中的文本文件的数据
- 002_005 Python 计算一个文件中有多少行即读取文件行数
- 读书笔记--python数据可视化--002_读取Excel文件数据