Python创建压缩文件

-----转载国外课栈网

Python允许您快速创建zip / tar存档。

以下命令将压缩整个目录

shutil.make_archive(output_filename,“zip”,DIR_NAME)

下面的命令让你控制上要存档,文件

ZipFile.write(filename)

在这里是在Python中创建Zip文件的步骤

步骤1) 要从Python创建存档文件,确保您的import语句正确且有序。这里存档的import语句 来自shutil import make_archive
在这里插入图片描述

代码解释

从模块shutil导入make_archive类

使用split函数将目录和文件名从文本文件位置的路径中拆分出来(guru99)

然后我们调用模块“shutil.make_archive(”guru99 archive,“zip”,root_dir)“创建存档文件,这将是zip格式

然后我们传入我们想要压缩的东西的根目录。所以目录中的一切将被压缩

当您运行代码时,您可以看到在面板右侧创建了存档zip文件。创建

步骤2)

存档文件后,您可以右键单击该文件并选择操作系统,它会显示你的存档文件中它如下图所示
在这里插入图片描述

现在您archive.zip文件将显示你的OS(Windows资源管理器)3)

步骤 3)当您在文件上双击,你会看到列表中的所有文件在那里。
在这里插入图片描述
在这里插入图片描述

步骤4) 在Python中,我们可以更好地控制归档,因为我们可以定义whi ch包含在归档中的特定文件。在我们的例子中,我们将在归档下包含两个文件 “guru99.txt” 和 “guru99.txt.bak”。

在这里插入图片描述

代码解释

从zip文件Python模块导入Zipfile类。这个模块完全控制创建zip文件

我们创建一个新Zipfile名为(“testguru99.zip,”w“)

创建一个新的Zipfile类,需要传入权限,因为它是一个文件,所以你需要以newzip将信息写入文件

我们使用变量“newzip”来引用我们创建的zip文件

使用“newzip”变量上的write函数,我们将文件“guru99.txt”和“guru99.txt.bak”添加到存档中

当你执行代码,你可以看到文件是在面板右侧创建的,名称为“guru99.zip”

注意:这里我们不给任何命令“关闭”文件,如“newzip.close”,因为我们使用“With“范围锁定,所以当程序超出此范围时,文件将被清理并自动关闭。

步骤5) 当您 - >右键单击文件(testguru99.zip)并 - >选择您的操作系统(Windows资源管理器),它将在文件夹中显示存档文件,如下所示。
在这里插入图片描述

当您双击文件“testguru99.zip”时,它将打开另一个窗口,并且将显示其中包含的文件。
在这里插入图片描述

下面是完整的代码
Python2代码:

import os

import shutil

from zipfile import ZipFile

from os import path

from shutil import make_archive



def main():

# Check if file exists

             if path.exists("guru99.txt"):

# get the path to the file in the current directory

            src = path.realpath("guru99.txt");

# rename the original file

            os.rename("career.guru99.txt","guru99.txt")

# now put things into a ZIP archive

            root_dir,tail = path.split(src)    

            shutil.make_archive("guru99 archive", "zip", root_dir)

# more fine-grained control over ZIP files

            with ZipFile("testguru99.zip","w") as newzip:

                     newzip.write("guru99.txt")    

                     newzip.write("guru99.txt.bak")

if __name__== "__main__":  

                  main()

Python3代码:

import os

import shutil

from zipfile import ZipFile

from os import path

from shutil import make_archive    

# Check if file exists      

             if path.exists("guru99.txt"):    

# get the path to the file in the current directory        

              src = path.realpath("guru99.txt");    

# rename the original file        

               os.rename("career.guru99.txt","guru99.txt")    

# now put things into a ZIP archive        

               root_dir,tail = path.split(src)        

               shutil.make_archive("guru99 archive","zip",root_dir)    

# more fine-grained control over ZIP files        

               with ZipFile("testguru99.zip", "w") as newzip:            

                        newzip.write("guru99.txt")            

                        newzip.write("guru99.txt.bak")




摘要

要压缩整个目录,请使用命令“shutil.make_archive(”name“,”zip“,root_dir)

要选择要压缩的文件,请使用命令”ZipFile.write(filename)“

猜你喜欢

转载自blog.csdn.net/jiyotin/article/details/86520260