项目.py编译.pyc脚本

import os
import py_compile

# 项目根目录
project_dir = os.getcwd()

# 需要忽略的文件和目录
ignore_files = ['gunicorn.conf.py']
ignore_dirs = ['venv']

def compile_and_cleanup(root_dir):
    for root, dirs, files in os.walk(root_dir):
        # 忽略指定目录
        dirs[:] = [d for d in dirs if d not in ignore_dirs]

        for file in files:
            # 获取文件的完整路径
            file_path = os.path.join(root, file)
            # 如果是.py文件且不在忽略列表中
            if file.endswith('.py') and file not in ignore_files:
                try:
                    # 编译.py文件为.pyc
                    py_compile.compile(file_path, cfile=file_path + 'c')
                    # 删除原始.py文件
                    os.remove(file_path)
                    print(f"Compiled and removed: {file_path}")
                except Exception as e:
                    print(f"Error compiling {file_path}: {e}")

# 执行编译和清理操作
compile_and_cleanup(project_dir)

 

posted @ 2024-09-06 15:43  Swlymbcty  阅读(4)  评论(0编辑  收藏  举报