1. 程式人生 > >python手動編譯py_compile,compileall

python手動編譯py_compile,compileall

python執行之後會自動生產pyc檔案,也可以手動編譯生成pyc檔案。程式碼如下:

 

#coding:utf-8
"""
2018-11-03
dinghanhua
手動編譯.pyc檔案
pyc檔案執行效率高且看不到原始碼
"""

import py_compile
import compileall
import os

filepath = os.getcwd() #獲取當前目錄
print('當前目錄:',filepath)

'''編譯單個檔案'''
py_compile.compile(filepath+'/demo.py') #僅編譯一個

filelist = os.listdir(filepath)  #
編譯目錄下指定的python檔案 print('當前目錄下所有檔案或資料夾:',filelist) for file in filelist: if file != '__init__.py' and file[-3:] == '.py': #編譯不是__init__.py的所有python檔案 py_compile.compile(filepath + '/'+file) print(file,'is compling') '''批量編譯''' compileall.compile_dir(filepath)

 

 

the end!