python--package the same type of files in the current directory into a compressed package

Usage scenario: When producing the same type of files in batches, you can use the following code to move all files to the target directory and pack them into a compressed package:

import shutil
import glob
import os
# ===========================================================================
def remover(path):
    """ param <path> could either be relative or absolute. """
    if os.path.isfile(path) or os.path.islink(path):
        os.remove(path)  # remove the file
    elif os.path.isdir(path):
        shutil.rmtree(path)  # remove dir and all contains
    else:
        raise ValueError("file {} is not a file or dir.".format(path))
# ===========================================================================
output_folder_name = 'folder_name'
copy = False
over_write_folder = True
# ===========================================================================
if os.path.exists(output_folder_name) and over_write_folder:
    remover(output_folder_name)
    
if not os.path.exists(output_folder_name):
    # 开文件夹
    os.makedirs(output_folder_name)
    # Copy 当前目录下的目标文件
    file_names = glob.glob("*.png") + glob.glob("*.xlsx")
    for file_name in file_names:
        if copy:
            shutil.copy(os.path.join('.', file_name), output_folder_name)
        else:
            shutil.move(os.path.join('.', file_name), output_folder_name)
    shutil.make_archive(f'{
      
      output_folder_name}', 'zip', output_folder_name)
else:
    print("改一下output_folder_name哈!")

Accumulation of knowledge points:

  • shutil模块: st+util, the meaning of shell tools. The shutil module is a supplement to the os module, mainly for copying, deleting, moving, compressing and decompressing operations of files.
    • shutil.rmtree: delete directory/folder;
    • shutil.move(source,destination): Move files, source is a file, destination can be a file or a directory;
    • shutil.copy(source,destination): Copy files, source is a file, destination can be a file or a directory;
    • shutil.make_archive(base_name, format, base_dir): base_name is the name of the created target file, format is the suffix of the compressed package format: zip, tar, bztar, gztar, base_dir is the path to start packaging;
  • glob模块: finds all pathnames matching a particular pattern according to the rules used by Unix terminals, but returns the results in an indeterminate order.
    • glob.glob(reg): reg is a regular expression, which can be directly filtered by file type and return a list;
  • os模块: Contains common operating system functions.
    • os.path.isfile(path): Determine whether the path is a file;
    • os.path.islink(path): Determine whether the path is a shortcut;
    • os.path.isdir(path): Determine whether the path is a directory;
    • os.path.exists(path): Determine whether the path exists;
    • os.path.join(pa,pb,pc): Refer to https://blog.csdn.net/qq_30815237/article/details/87903364 for connection rules;
    • os.remove(path):Delete Files;
    • os.makedirs(folder_name): Create a folder/subdirectory under the current directory;

Guess you like

Origin blog.csdn.net/zyl_wjl_1413/article/details/125574418