遍历 文件夹,如果是小的zip到一个zip,并且转移,如果是大的,不动,打印文件大小

import os

# ignore_dirs=['node_modules',".git"]
# ,"js"
ignore_dirs=['node_modules',]


def have_in(path):
    for i in ignore_dirs:
        if i  in path:
            return True
    return False

def path_size(entry_path):
    total_size = 0
    for path, dirs, files in os.walk(entry_path):
        # print(path)
        # node_modules
        
        if have_in(path):
            # print(f"Skipping folder: {entry_path}")
            print(f"Skipping folder: {path}")
            # return total_size
            return 999999999999
            # continue
        # if "git-cache-vue3" in path:
        #     print(path)
        # "D:\proj\vue\git-cache-vue3"
        if "node_modules" in path:
            # print(f"Skipping folder: {path}")
            continue
        # print(files)
        
        if path=="node_modules":
            # print(f"Skipping folder: {path}")
            continue
        for f in files:
            if f=="node_modules":
                print(f"Skipping folder: {f}")
                continue
            fp = os.path.join(path, f)
            total_size += os.path.getsize(fp)
    return total_size

def convert_size(size_bytes):
    # 转换单位函数
    units = ["bytes", "KB", "MB", "GB", "TB"]
    unit_index = 0
    while size_bytes >= 1024 and unit_index < len(units) - 1:
        size_bytes /= 1024
        unit_index += 1
    size_formatted = f"{size_bytes:.2f} {units[unit_index]}"
    return size_formatted

def get_folder_size(folder_path):
    total_size = 0
    for path, dirs, files in os.walk(folder_path):
        for f in files:
            fp = os.path.join(path, f)
            total_size += os.path.getsize(fp)
    return total_size

vue_proj_small_dir=rf"D:\file\vue_proj_small"
os.makedirs(vue_proj_small_dir,exist_ok=True)
import zipfile
destination_folder=vue_proj_small_dir
def zip_folders(folder_path, zip_file):
    with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zipf:
        entries = os.listdir(folder_path)
        for entry in entries:
            entry_path = os.path.join(folder_path, entry)
            if os.path.isdir(entry_path):
                total_size = get_folder_size(entry_path)
                if total_size <= 5 * 1024 * 1024:  # 小于等于5MB
                    for root, dirs, files in os.walk(entry_path):
                        for file in files:
                            file_path = os.path.join(root, file)
                            zipf.write(file_path, arcname=os.path.relpath(file_path, folder_path))
                    shutil.move(entry_path, destination_folder)
# FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'D:\\proj\\vue\\git-cache-vue3\\node_modules\\@babel\\core\\node_modules\\@babel\\helper-module-transforms\\node_modules\\@babel\\template\\node_modules\\@babel\\code-frame\\node_modules\\@babel\\highlight\\node_modules\\@babel\\helper-validator-identifier\\scripts\\generate-identifier-regex.js'

import shutil

def get_folder_sizes(folder_path,zip_file):
    if folder_path=="node_modules":
        print(f"Skipping folder: {folder_path}")
        return 
    with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zipf:
        folder_sizes = {}
        entries = os.listdir(folder_path)
        # print(entries)
        for entry in entries:
            entry_path = os.path.join(folder_path, entry)
            if os.path.isdir(entry_path):
                if entry == "node_modules":
                    print(f"Skipping folder: {entry_path}")
                    continue
                total_size=path_size(entry_path)
                if total_size <= 5 * 1024 * 1024:  # 小于等于5MB
                    print("entry_path 小于等于5MB")
                    print(entry_path)
                    for root, dirs, files in os.walk(entry_path):
                        for file in files:
                            file_path = os.path.join(root, file)
                            zipf.write(file_path, arcname=os.path.relpath(file_path, folder_path))
                    shutil.move(entry_path, destination_folder)
             
                folder_sizes[entry_path] = total_size
                # print(folder_sizes)
                # print(entry_path,total_size)
                size_formatted = convert_size(total_size)
                print(f"{entry_path}: {size_formatted}")
    return folder_sizes

def main():
    
    folder_path = rf"D:\proj\vue2" 
    # folder_path = rf"D:\proj\vue"  # 替换为你要查询的文件夹路径
    folder_sizes = get_folder_sizes(folder_path,zip_file=rf"D:\file\vue_proj_zip/vue_proj_zip.zip")
    sorted_sizes = sorted(folder_sizes.items(), key=lambda x: x[1], reverse=True)
    print("========== all dir sizes")
    for folder, size in sorted_sizes:
        size_formatted = convert_size(size)
        print(f"{folder}: {size_formatted}")
    print("destination_folder",destination_folder)

if __name__ == '__main__':
    main()

你可能感兴趣的:(tool,python)