【问题标题】:Python: In PyCharm, there are three files I want to compress to a .zip file. How do I do this?Python:在 PyCharm 中,我想将三个文件压缩为 .zip 文件。我该怎么做呢?
【发布时间】:2021-12-19 09:13:24
【问题描述】:

这是三个文件名(在这个例子中,里面的内容应该无关紧要):

File1.txt
File2.txt
File3.txt

这是我当前的代码:

# create new .txt files
newFile1 = open('File1.txt', 'w')
newFile2 = open('File2.txt', 'w')
newFile3 = open('File3.txt', 'w')

# compress these three newly created files into a .zip folder

我的目标是在脚本执行时创建一个压缩上面列出的三个文件的 .zip 文件。

这是我想要的输出(一个新创建的 .zip 文件):

CompressedFolder.zip

【问题讨论】:

标签: python


【解决方案1】:

你应该可以使用:

import zipfile 
# your code here
with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')

在这里:https://docs.python.org/3/library/zipfile.html

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多