相关疑难解决方法(0)

使用ZipFile模块从zipfile中删除文件

我从zipfile中删除文件的唯一方法是创建一个临时zipfile而不删除该文件,然后将其重命名为原始文件名.

在python 2.4中,ZipInfo类有一个属性file_offset,因此可以创建第二个zip文件并将数据复制到其他文件而不进行解压缩/重新压缩.

file_offset在python 2.6中是缺失的,所以除了通过解压缩每个文件然后再次重新压缩它来创建另一个zipfile之外还有另一种选择吗?

是否有可能直接删除zipfile中的文件,我搜索并没有找到任何东西.

python zip delete-file

35
推荐指数
4
解决办法
2万
查看次数

如何使用python更新zip文件中的一个文件

我有这个zip文件结构.

zipfile name = filename.zip

filename>    images>
             style.css
             default.js
             index.html
Run Code Online (Sandbox Code Playgroud)

我想只更新index.html.我试图更新index.html,但它只包含1.zip文件中的index.html文件,其他文件被重新驱动.

这是我试过的代码:

import zipfile

msg = 'This data did not exist in a file before being added to the ZIP file'
zf = zipfile.ZipFile('1.zip', 
                     mode='w',
                     )
try:
    zf.writestr('index.html', msg)
finally:
    zf.close()

print zf.read('index.html')
Run Code Online (Sandbox Code Playgroud)

那么如何才能使用Python更新index.html文件呢?

python zipfile

9
推荐指数
2
解决办法
2万
查看次数

Python的zipfile模块无法更新条目

我想使用pythons zipfile模块更新zip文件中的条目.我的问题是,这会生成一个新条目.

请假设我有这个代码:

from zipfile import ZipFile,ZIP_DEFLATED
with ZipFile("myfile.zip","w") as z:
    z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
    ###  how to update the hello.txt file here ?
    z.writestr("hello.txt", "the content of hello.txt", ZIP_DEFLATED)
Run Code Online (Sandbox Code Playgroud)

在此之后,实际的zip文件有两个条目而不是一个:

$ unzip -l myfile.zip 
Archive:  myfile.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       24  2013-02-19 22:48   hello.txt
       24  2013-02-19 22:48   hello.txt
---------                     -------
       48                     2 files
$ python --version
Python 3.3.0
$
Run Code Online (Sandbox Code Playgroud)

我知道编写一个完整的新文件的方法,但如果内容很大,这将花费很多时间.

拉链(1)实用程序可以做到这一点(使用"-u"选项),那么为什么不蟒蛇?有什么方法我仍然可以使用python实现这一点?

谢谢

python zipfile

4
推荐指数
1
解决办法
1356
查看次数

使用python从word文件中的注释中删除个人信息

我想从word文件中的评论中删除所有个人信息。

删除作者姓名很好,我使用以下内容做到了这一点,

document = Document('sampleFile.docx')
core_properties = document.core_properties
core_properties.author = ""
document.save('new-filename.docx')
Run Code Online (Sandbox Code Playgroud)

但这不是我需要的,我想删除在该单词文件中发表评论的任何人的姓名。

我们手动执行此操作的方法是进入首选项-> 安全性-> 保存时从此文件中删除个人信息

python python-docx

4
推荐指数
2
解决办法
1988
查看次数

Python无法处理zipfile.BadZipFile中的异常

如果zip文件已损坏,则需要处理,因此它只需传递此文件即可继续下一个。

在Im试图捕获异常的下面的代码示例中,我可以通过它。但是,当zip文件损坏时,我的脚本失败了,并给了我“正常”的追溯错误*,而不是打印了“我的错误”,但是如果zip文件正常,则脚本运行正常。

这是我正在处理的代码的一个简约示例。

path = "path to zipfile" 

from zipfile import ZipFile

with ZipFile(path) as zf:
    try:
        print "zipfile is OK"
    except BadZipfile:
        print "Does not work "
        pass
Run Code Online (Sandbox Code Playgroud)

追溯的一部分告诉我:引发BadZipfile,“文件不是zip文件”

python exception-handling zipfile python-2.7

1
推荐指数
1
解决办法
8263
查看次数