import os, shutil
import compileall
def copy_to_up(path):
for f in os.listdir(path):
if f == '__pycache__':
for f_main in os.listdir(os.path.join(path, f)):
path_src = os.path.join(path, f)
shutil.copy(os.path.join(path_src,f_main),path) #copy src up to path
filePath = os.path.join(path, f)
if os.path.isfile(filePath):
os.remove(filePath)
print("[kongdejing debug] " + filePath + " was removed!") #delete file
elif os.path.isdir(filePath):
shutil.rmtree(filePath,True)
print("[kongdejing debug] " + "Directory: " + filePath +" was removed!") #delete directory
def rename_file(path):
for f in os.listdir(path):
if f.endswith('.pyc'):
f_new = f.replace('.cpython-39','')
os.rename(os.path.join(path, f), os.path.join(path, f_new))
def copy_rename(f, path):
# 如果是__pycache__ 文件夹 将其中的文件移至上一层
if f == '__pycache__':
copy_to_up(path)
#当前层为.pyc文件重命名
rename_file(path)
for pyf in os.listdir(path):
if pyf.endswith('.py') and not os.path.isdir(os.path.join(path, pyf)):
os.remove(os.path.join(path, pyf))
def recursion_search(path):
for f_d in os.listdir(path):
copy_rename(f_d, path)
if os.path.isdir(os.path.join(path, f_d)):
path_d_d = os.path.join(path, f_d)
recursion_search(path_d_d)
def py_to_pyc(path):
for f in os.listdir(path):
copy_rename(f, path)
if f != 'env' and f!='.vscode' and os.path.isdir(os.path.join(path, f)):
path_d = os.path.join(path, f)
print(path_d)
recursion_search(path_d)
if __name__ == '__main__':
path = r'./docker_release'
# path = r'./test'
compileall.compile_dir(path, force=True)
py_to_pyc(path)
优化python 批量转化pyc脚本
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 在上一篇中,写了如何获取某篇英文文献pdf的参考文献信息。【python实战】获取英文文献pdf中参考文献信息[h...
- 第一步 安装python,自己windows系统的电脑直接安装Anaconda3即可 第二步 安装 PyPDF2 ...
- 本文介绍基于EndNote等软件在Word中插入参考文献后,将中文参考文献中的et al.替换为等的方法。 ...
- 通过sips命令一键批量生成APP启动画面的所有尺寸,包含IOS和Android。 关于命令代表的相关知识可以通过...
- Python v3.7.0 / Airtest: 1.1.1 / PocoUI: 1.0.78其他笔记:Airte...