__author__ = 'wei' # -*- coding: utf-8 -*- __author__ = 'wei' import getopt import sys import os import py_compile def setparams(): global srcpath,pycpath ps=getopt.getopt(sys.argv[1:], '', ['srcpath=', 'pycpath=']) psdict=dict(ps[0]) if '--srcpath' in psdict: srcpath=psdict['--srcpath'] else: srcpath=input('请输入源文件夹:') if '--pycpath' in psdict: pycpath=psdict['--pycpath'] else: pycpath=input('请输入目标pyc文件夹:') if __name__=='__main__': srcpath='' pycpath='' setparams() print(srcpath) for root,dirs,files in os.walk(srcpath): for file in files: if file[-3:]=='.py': pyname=root+os.path.sep+file pycname=pyname.replace(srcpath,pycpath)+'c' pycdir=pycname.replace(file+'c', '') if not os.path.exists(pycdir): #if not exist dir,create it os.mkdir(pycdir) print(pyname,pycname) py_compile.compile(pyname,cfile=pycname)