GVKun编程网logo

python zip文件 压缩(python zip压缩文件夹)

3

对于想了解pythonzip文件压缩的读者,本文将是一篇不可错过的文章,我们将详细介绍pythonzip压缩文件夹,并且为您提供关于CorePython|2-CorePython:GettingSta

对于想了解python zip文件 压缩的读者,本文将是一篇不可错过的文章,我们将详细介绍python zip压缩文件夹,并且为您提供关于Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ios – 如何从我的文档目录创建.zip文件?、ios – 设置了正确的内容类型,但是.ipa文件被视为.zip文件的有价值信息。

本文目录一览:

python zip文件 压缩(python zip压缩文件夹)

python zip文件 压缩(python zip压缩文件夹)

从简单的角度来看的话,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()
f.close()
不知道上面的例子是不是足够简单呢?
1.1 zipfile.ZipFile(fileName[, mode[, compression[, allowZip64]]])
fileName是没有什么疑问的了。
mode和一般的文件操作一样,’r’表示打开一个存在的只读ZIP文件;’w’表示清空并打开一个只写的ZIP文件,或创建一个只写的ZIP文件;’a’表示打开一个ZIP文件,并添加内容。
compression表示压缩格式,可选的压缩格式只有2个:ZIP_STORE;ZIP_DEFLATED。ZIP_STORE是默认的,表示不压缩;ZIP_DEFLATED表示压缩,如果你不知道什么是Deflated,那么建议你去补补课。
allowZip64为True时,表示支持64位的压缩,一般而言,在所压缩的文件大于2G时,会用到这个选项;默认情况下,该值为False,因为Unix系统不支持。
1.2 zipfile.close()
说真的,这个没什么可说的,如果有的话,那就是你写入的任何文件在关闭之前不会真正写入磁盘。
1.3 zipfile.write(filename[, arcname[, compress_type]])
acrname是压缩文件中该文件的名字,默认情况下和filename一样
compress_type的存在是因为zip文件允许被压缩的文件可以有不同的压缩类型。
1.4 zipfile.extractall([path[, member[, password]]])
path解压缩目录,没什么可说的
member需要解压缩的文件名儿列表
password当zip文件有密码时需要该选项
对于简单的应用,这么多就够了。
2)高级应用
2.1 zipfile.is_zipfile(filename)
判断一个文件是不是压缩文件
2.2 ZipFile.namelist()
返回文件列表
2.3 ZipFile.open(name[, mode[, password]])
打开压缩文档中的某个文件
2.4 ZipFile.infolist()
2.5 ZipFile.getinfo(name)
上述文件返回ZipInfo对象,只不过一个返回的是列表,一个返回的是一个ZipInfo
ZipInfo类
2.6 ZipInfo.filename
2.7 ZipInfo.date_time
返回值的格式为(year,month,date,hour,minute,second)
2.8 ZipInfo.compress_type
2.9 ZipInfo.comment
2.10ZipInfo.extra
2.11ZipInfo.create_system
2.12ZipInfo.extract_version
2.13ZipInfo.reserved 总是0
2.14ZipInfo.flag_bits
2.15ZipInfo.volume
2.16ZipInfo.internal_attr
2.17ZipInfo.external_attr
2.18ZipInfo.header_offset
2.19ZipInfo.CRC
2.20ZipInfo.file_size
2.21ZipInfo.compress_size
2.22ZipFile.testzip()
检查每个文件和它对应的CRC,如果有错误返回对应的文件列表
2.23ZipFile.setpassword(password)
2.24ZipFile.read(name[,password])
返回对应的文件
2.25ZipFile.printdir()
打印压缩文件夹的信息
2.26ZipFile.writestr(zipinfo_or_arcname, bytes)
PyZipFile类
zipfile.PyZipFile除了上面的方法和属性之外,还有一个特殊的方法
2.27PyZipFile.writepy(pathname,basename)
一般情况下,仅仅压缩.pyc和.pyo文件,不压缩.py文件
———————————————————————-
zip文件格式信息
一个 ZIP 文件由三个部分组成:压缩源文件数据区+压缩源文件目录区+压缩源文件目录结束标志
1)压缩源文件数据区
在这个数据区中每一个压缩的源文件/目录都是一条记录,记录的格式如下: [文件头+ 文件数据 + 数据描述符]
   a、文件头结构
   组成 长度
   文件头标记 4 bytes (0x04034b50)
   解压文件所需 pkware 版本 2 bytes
   全局方式位标记 2 bytes
   压缩方式 2 bytes
   最后修改文件时间 2 bytes
   最后修改文件日期 2 bytes
   CRC-32校验 4 bytes
   压缩后尺寸 4 bytes
   未压缩尺寸 4 bytes
   文件名长度 2 bytes
   扩展记录长度 2 bytes
   文件名 (不定长度)
   扩展字段 (不定长度)
  
   b、文件数据
  
   c、数据描述符
  组成 长度
   CRC-32校验 4 bytes
   压缩后尺寸 4 bytes
   未压缩尺寸 4 bytes
   这个数据描述符只在全局方式位标记的第3位设为1时才存在(见后详解),紧接在压缩数据的最后一个字节后。这个数据描述符只用在不能对输出的 ZIP 文件进行检索时使用。例如:在一个不能检索的驱动器(如:磁带机上)上的 ZIP 文件中。如果是磁盘上的ZIP文件一般没有这个数据描述符。
  
2)压缩源文件目录区
   在这个数据区中每一条纪录对应在压缩源文件数据区中的一条数据
   组成 长度
   目录中文件文件头标记 4 bytes (0x02014b50)
   压缩使用的pkware 版本 2 bytes
   解压文件所需 pkware 版本 2 bytes
   全局方式位标记 2 bytes
   压缩方式 2 bytes
   最后修改文件时间 2 bytes
   最后修改文件日期 2 bytes
   CRC-32校验 4 bytes
   压缩后尺寸 4 bytes
   未压缩尺寸 4 bytes
   文件名长度 2 bytes
   扩展字段长度 2 bytes
   文件注释长度 2 bytes
   磁盘开始号 2 bytes
   内部文件属性 2 bytes
   外部文件属性 4 bytes
   局部头部偏移量 4 bytes
   文件名 (不定长度)
   扩展字段 (不定长度)
   文件注释 (不定长度)
  
3)压缩源文件目录结束标志
   组成 长度
   目录结束标记 4 bytes (0x02014b50)
   当前磁盘编号 2 bytes
   目录区开始磁盘编号 2 bytes
   本磁盘上纪录总数 2 bytes
   目录区中纪录总数 2 bytes
   目录区尺寸大小 4 bytes
   目录区对第一张磁盘的偏移量 4 bytes
   ZIP 文件注释长度 2 bytes
   ZIP 文件注释 (不定长度)

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod

It's important to understand the Python execution model and precisely when function deFinitions and other important events occur when a module is imported or executed. Here, we show execution of our Python module as it's imported in a graphical debugging environment. We step through the top‑level statements in the module. What's important to realize here is that the def used for the fetch_words function isn't merely a declaration. It's actually a statement, which when executed in sequence with the other top‑level model scope code, causes the code within the function to be bound to the name of the function. When modules are imported or run, all of the top‑level statements are run, and this is the means by which the function within the module namespace are defined. We are sometimes asked about the difference between Python modules, Python scripts, and Python programs. Any .py file constitutes a Python module. But as we've seen, modules can be written for convenient import, convenient execution, or using the if dunder name = dunder main idiom, both. We strongly recommend making even simple scripts importable since it eases development and testing so much if you can access your code from within the REPL. Likewise, even modules, which are only ever meant to be imported in production settings, benefit from having executable test code. For this reason, nearly all modules we create have this form of defining one or more importable functions with a postscript to facilitate execution. Whether you consider a module to be a Python script or Python program is a matter of context and usage. It's certainly wrong to consider Python to be merely a scripting tool, in the vein of Windows batch files or UNIX Shell scripts, as many large and complex applications are built exclusively with python.

 

  • def不仅仅是一个declaration声明,更是一条statement语句。它将其中的python代码于函数名绑定在一起
  • 一个py文件就是一个模块,这个模块包含类或函数。你写python,要尽量将代码包装成函数和类,方便各种import
  • 一个py文件也可看作是一个脚本,在系统命令行中运行
  • python不仅仅是脚本语言,很多大型程序都是用python构建的

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

Error: Can‘t find Python executable “python“, you can set the PYTHON env variable

在启动vue项目的时候,安装node.js组件node-sass过程中报错了,错误提示如下
Error: Can’t find Python executable “python”, you can set the PYTHON env variable

由错误提示可知:Node.js 在安装模块组件node-sass的时候,node.js缺少Visual Studio2015 Build Tools相关的组件和python的环境,如果安装了vs2015组件的小伙伴们就不用安装Visual Studio2015 Build Tools相应的组件,只用安装python2.7即可解决缺少的python组件的问题。

欲安装python2.7,请至python官网:www.python.org 下载,然后配置好python的环境变量即可。

不过博主我并不推荐上述的解决方案,因为对于程序员来说,效率第一,上述的问题一个命令就可以轻松解决你所遇到的麻烦,前面说了那么多,无非就是想告诉在看本篇博客的同仁们放下浮躁的心,遇到问题首先不是急着去解决问题,而是分析为什么会这样,然后才能水到聚成的去找到解决问题的方法。

运行下面这个命令即可解决你们遇到的Error问题

npm install --global --production windows-build-tools

:上面讲述了一堆就是为了讲述此命令是干嘛的,上面已经描述很详细了,就不再赘述了,该操作与上述的一堆操作无异,效果却是一样的。

然后运气不好的小伙伴可能接着会遇到一个坑,那就是执行了:npm install --global --production windows-build-tools这个命令的人细心点会发现执行到一半就卡住了,这个卡住了没有红字重点提示,而且下方还有英文在等待中,粗心的小伙伴可能以为是命令执行完了,组件安装好了,其实不然,我这边已经解决了,就无法复现了,具体点就是中文的提示,提示我们由于有类似组件在运行或者下载导致无法继续下载安装组件了。稳妥点的解决办法是,将电脑重启,将底层正在运行的模块干掉,待电脑重启后再执行npm install --global --production windows-build-tools这条命令即可,博主我就是这样解决的,稳稳的幸福就会浮现在你面前如下图所示,你的可能和我不一样,因为我已经跑成功过一次了,没有你的那么多细节的log打印。
在这里插入图片描述

然后就是在你的项目下shift+鼠标右击你的项目运行npm run dev即可启动vue项目了。

ios – 如何从我的文档目录创建.zip文件?

ios – 如何从我的文档目录创建.zip文件?

我想从我的文档目录创建一个ZIP文件.

我也想把它作为电子邮件附件发送.

解决方法

ZipArchive是一个压缩或解压缩zip文件的Objective-C类,它基于开源代码“MiniZip”.

它可用于iPhone应用程序开发,也可用于Mac OSX上的cocoa.

看到这个:http://code.google.com/p/ziparchive/downloads/list

ios – 设置了正确的内容类型,但是.ipa文件被视为.zip文件

ios – 设置了正确的内容类型,但是.ipa文件被视为.zip文件

我写了一个小的Rails应用程序,我可以上传三个文件,我需要通过无线方式分发adhoc构建.这包括.ipa文件,.mobileprovisioning文件和.plist文件.

问题是现在,当我点击.ipa文件的链接时,文件被视为正常下载,我的iDevices会询问我存储文件的位置.

curl -I example.com/path/to/App.ipa

生成此输出

HTTP/1.1 200 OK
Date: Thu,25 Oct 2012 13:55:08 GMT
Cache-Control: public,max-age=0
Last-Modified: Thu,25 Oct 2012 10:23:07 GMT
ETag: "742-1351160587000"
Content-Type: application/octet-stream
Accept-Ranges: bytes
Content-Length: 742
Connection: keep-alive

我认为application / octet-stream是正确的内容类型.我究竟做错了什么?

提前感谢您的回答

问候,
布克

解决方法

.ipa文件只是一个(不是非常好的伪装)zip文件(它刚刚重命名为.ipa).也许当你设置通用的application / octet-stream MIME类型时,iOS上的Safari浏览器会查看文件的实际内容,发现它实际上是一个ZIP存档并继续.顺便说一句,在我看来,你想要做一些内部或临时分发的iOS应用程序.在这种情况下,您应该将用户引向manifest.plist文件(以itms-services://开头的URL),以便iOS知道它需要查找应用程序包然后下载并安装它.

Documentation here.

关于python zip文件 压缩python zip压缩文件夹的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Core Python | 2 - Core Python: Getting Started | 2.5 - Modularity | 2.5.5 - The Python Execution Mod、Error: Can‘t find Python executable “python“, you can set the PYTHON env variable、ios – 如何从我的文档目录创建.zip文件?、ios – 设置了正确的内容类型,但是.ipa文件被视为.zip文件的相关知识,请在本站寻找。

本文标签: