Python3脚本批量压缩文件夹

#!/usr/bin/pyton3

# coding=utf-8

# author happygoes


import os

import sys

import platform


def compress_files():

'''压缩函数

将目录下面的文件夹挨个压缩为7z文件,系统必须安装7z软件

Windows下运行需带7Zip文件夹及里面工具

'''

    if platform.system() == "Windows":

        filelists = []

            string = os.popen("dir").read()

            filelist = string.split("\n")[7:-3]

            for file in filelist:

                file = file.split(" ")[-1:]

                filelists.extend(file)

            for file in filelists:

                if os.path.isdir(file) and file != "7Zip":

                    cmd = "7Zip\\7z.exe a -r " + file + ".7z" + " " + file

                    print(os.popen(cmd).read())

    elif platform.system() == "Linux":

        string = os.popen("ls").read()

        filelists = string.split("\n")

        filelists = filelists[:-1]

        print(filelists)

        for file in filelists:

        if os.path.isdir(file) and file != "7Zip":

            cmd = "7za a -t7z -r " + file + ".7z" + " " + file + "/*"

            print(os.popen(cmd).read())


if __name__ == "__main__":

    compress_files()

 

你可能感兴趣的:(Python3实用小脚本)