贴一个自己写的,递归当前目录下以及子目录下所有的.py文件 并编译为 .pyo文件 最后打包成一个 .zip压缩包。
#coding:utf-8
import os,zipfile
def zip_files( files, zip_name ):
zip = zipfile.ZipFile( zip_name, ‘w’, zipfile.ZIP_DEFLATED )
for file in files:
print (‘compressing’, file)
zip.write( file )
zip.close()
print (‘compressing finished’)
def getallfiles(path):
allfile=[]
for dirpath,dirnames,filenames in os.walk(path):
# for dir in dirnames:
#allfile.append(os.path.join(dirpath,dir))
for name in filenames:
if name!=’make.py’ and name.endswith(".py"):
allfile.append(os.path.join(dirpath, name))
if name == ‘make.py’:
os.system("python -O -m py_compile " + os.path.join(dirpath, name))
return allfile
if __name__ == ‘__main__’:
zipfiles=[]
path = os.path.dirname(__file__)
allfile=getallfiles(path)
for file in allfile:
print file
os.system("python -O -m py_compile " + file)
portion = os.path.splitext(file)
zipfiles.append(portion[0].replace(path,’.’)+’.pyo’)
zip_file = ‘.\\ip.zip’#压缩包名字
zip_files(zipfiles, zip_file)