Requirements describe

Write a Python program that automatically decompresses the internal files to the current folder and deletes the compressed files each time you download a file in the form of a compressed package.

  • Integrated application of OS modules

  • Glob module integrated application

  • Extract files using the gzip zipfile rarfile tarfile module

Image uploading failed

retry

Step analysis and prior knowledge

The code code needs to interpret complex problems into multiple explicit requirements, that is, the program implements the logic as follows:

  1. Periodically check whether compressed files exist in a folder (for example, the Download folder)

  2. If there is a new compressed file, unzip it to a new folder in the current folder to delete the compressed file

  3. When it comes to compressed files, different compression formats are discussed. There are mainly the following four kinds:

When it comes to compressed files, different compression formats are discussed. There are mainly the following four kinds:

Gz: is similar to.tar.gz, but can be used to compress multiple files. Rar: Packaged compressed files, originally used for DOS

Therefore, the logic for determining whether a file is compressed can be as follows:

  1. Create a list of compressed_lst suffixes compressed_lst = [‘gz’, ‘tar’, ‘zip’, ‘rar’]

  2. Run filename.split(.) for all file names in the destination folder. [-1] Gets the suffix name

  3. Determine if the suffix is in compressed_lst, and if so, run the subsequent decompression code

  4. If the file ends in gz, check whether the file ends in. Tar after decompressing the file and handle the file accordingly

The decompression codes of different compressed files are different, which will be expanded in the subsequent code operations.

Code implementation

The first step is to get the names of all the files in the Download folder

Import glob import OS path = r 'c :\ XXXX \download' file_lst = glob.glob(path + '/*') # filename_lst = [os.path.basename(i) for i in file_lst] print(filename_lst)Copy the code

Next, look at the code framework to determine whether compression is required based on the file name extension

For I in filename_lst: if '.' in I: # get suffix = i.split('.')[-1] # match suffix == 'gz': pass if... Pass if suffix == 'tar': pass if suffix == 'zip': pass if suffix == 'rar': passCopy the code

There are two caveats:

  1. Filename.split (‘.’)[-1] will give an error, so you need to check first

  2. Gz ending files, after decompression, need to determine again whether it is ended in.tar, and then we can respectively decompress the four kinds of compressed files into functions, which can be called separately when it meets

1. Process. Gz files

import gzip def ungz(filename): Gz_file = gzip.GzipFile(filename) with open(filename, "W +") as file: file.write(gzfile.read ()) return filename # This gzip function needs to return a value to further match the untar functionCopy the code

The gz file repeatedly mentioned before may work with the tar file. Therefore, determine whether to unlock the tar file after decompressing the gz file

Here we can write the function of the tar file first

2. Process the. Tar file

import tarfile def untar(filename): Tar = tarfile.open(filename) names = tar.getnames() # tar = tar.getnames() # tar = tarfile.open(filename) names = tar.getnames() # tar If not os.path.isdir(filename + "_dir"): os.mkdir(filename + "_dir") for name in names: tar.extract(name, filename + "_dir/") tar.close()Copy the code

3. Process. Zip files

import zipfile def unzip(filename): Isdir (filename + "_dir"): if not os.path.isdir(filename + "_dir"): os.mkdir(filename + "_dir") for names in zip_file.namelist(): zip_file.extract(names, filename + "_dir/") zip_file.close()Copy the code

4. Processing. Rar files

import rarfile


def unrar(filename):
    rar = rarfile.RarFile(filename)
    if not os.path.isdir(filename + "_dir"):
        os.mkdir(filename + "_dir")
    os.chdir(filename + "_dir")
    rar.extractall()
    rar.close()
Copy the code

Remove () can be used to remove the compressed package. Now look at the contents of the logical framework after the decompression function is added:

for filename in filename_lst:
    if '.' in filename:
        suffix = filename.split('.')[-1]
        if suffix == 'gz':
            new_filename = ungz(filename)
            os.remove(filename)
            if new_filename.split('.')[-1] == 'tar':
                untar(new_filename)
                os.remove(new_filename)  
        if suffix == 'tar':
            untar(filename)
            os.remove(filename)
        if suffix == 'zip':
            unzip(filename)
            os.remove(filename)
        if suffix == 'rar':
            unrar(filename)
            os.remove(filename)
Copy the code

5. Real-time detection

A simple way to do this is to create a while True loop with time.sleep() as follows:

Import time while True: func() time.sleep(5Copy the code

Finally, change the implementation code of the second step into a function and put it in the loop framework to complete this requirement. The complete code is as follows

import glob
import os
import gzip
import tarfile
import zipfile
import rarfile
import time


path = r'C:\xxxx\download'
file_lst = glob.glob(path + '/*')
filename_lst = [os.path.basename(i) for i in file_lst]


def ungz(filename):
    filename = filename[:-3]
    gz_file = gzip.GzipFile(filename)
    with open(filename, "w+") as file:
        file.write(gz_file.read())
    return filename


def untar(filename):
    tar = tarfile.open(filename)
    names = tar.getnames()
    if not os.path.isdir(filename + "_dir"):
        os.mkdir(filename + "_dir")
    for name in names:
        tar.extract(name, filename + "_dir/")
    tar.close()


def unzip(filename):
    zip_file = zipfile.ZipFile(filename)
    if not os.path.isdir(filename + "_dir"):
        os.mkdir(filename + "_dir")
    for names in zip_file.namelist():
        zip_file.extract(names, filename + "_dir/")
    zip_file.close()


def unrar(filename):
    rar = rarfile.RarFile(filename)
    if not os.path.isdir(filename + "_dir"):
        os.mkdir(filename + "_dir")
    os.chdir(filename + "_dir")
    rar.extractall()
    rar.close()


def unzip_files():
    for filename in filename_lst:
        if '.' in filename:
            suffix = filename.split('.')[-1]
            if suffix == 'gz':
                new_filename = ungz(filename)
                os.remove(filename)
                if new_filename.split('.')[-1] == 'tar':
                    untar(new_filename)
                    os.remove(new_filename)
            if suffix == 'tar':
                untar(filename)
                os.remove(filename)
            if suffix == 'zip':
                unzip(filename)
                os.remove(filename)
            if suffix == 'rar':
                unrar(filename)
                os.remove(filename)


while True:
    unzip_files()
    time.sleep(5)
Copy the code

Afterword.

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join the discussion and answer resource base