python多文件打包
❶ python中pyinstaller打包多个py文件的问题
两个答案都是否
❷ 利用python编程,在多个打包压缩的文件中搜索指定字符串。有很多xml文件
ziprar.py
__author__='williezh'
#!/usr/bin/envpython3
importos
importsys
importtime
importshutil
importzipfile
fromzipfileimportZIP_DEFLATED
#Zip文件处理类
classZFile(object):
def__init__(self,fname,mode='r',basedir=''):
self.fname=fname
self.mode=mode
ifself.modein('w','a'):
self.zfile=zipfile.ZipFile(fname,mode,compression=ZIP_DEFLATED)
else:
self.zfile=zipfile.ZipFile(fname,self.mode)
self.basedir=basedir
ifnotself.basedir:
self.basedir=os.path.dirname(fname)
defaddfile(self,path,arcname=None):
path=path.replace('//','/')
ifnotarcname:
ifpath.startswith(self.basedir):
arcname=path[len(self.basedir):]
else:
arcname=''
self.zfile.write(path,arcname)
defaddfiles(self,paths):
forpathinpaths:
ifisinstance(path,tuple):
self.addfile(*path)
else:
self.addfile(path)
defclose(self):
self.zfile.close()
defextract_to(self,path):
forpinself.zfile.namelist():
self.extract(p,path)
defextract(self,fname,path):
ifnotfname.endswith('/'):
fn=os.path.join(path,fname)
ds=os.path.dirname(fn)
ifnotos.path.exists(ds):
os.makedirs(ds)
withopen(fn,'wb')asf:
f.write(self.zfile.read(fname))
#创建Zip文件
defcreateZip(zfile,files):
z=ZFile(zfile,'w')
z.addfiles(files)
z.close()
#解压缩Zip到指定文件夹
defextractZip(zfile,path):
z=ZFile(zfile)
z.extract_to(path)
z.close()
#解压缩rar到指定文件夹
defextractRar(zfile,path):
rar_command1="WinRAR.exex-ibck%s%s"%(zfile,path)
rar_command2=r'"C:WinRAR.exe"x-ibck%s%s'%(zfile,path)
try:
res=os.system(rar_command1)
ifres==0:
print("PathOK.")
except:
try:
res=os.system(rar_command2)
ifres==0:
print("Successtounrarthefile{}.".format(path))
except:
print('Error:cannotunrarthefile{}'.format(path))
#解压多个压缩文件到一个临时文件夹
defextract_files(file_list):
newdir=str(int(time.time()))
forfninfile_list:
subdir=os.path.join(newdir,fn)
ifnotos.path.exists(subdir):
os.makedirs(subdir)
iffn.endswith('.zip'):
extractZip(fn,subdir)
eliffn.endswith('.rar'):
extractRar(fn,subdir)
returnnewdir
#查找一个文件夹中的某些文件,返回文件内容包含findstr_list中所有字符串的文件
deffindstr_at(basedir,file_list,findstr_list):
files=[]
forr,ds,fsinos.walk(basedir):
forfninfs:
iffninfile_list:
withopen(os.path.join(r,fn))asf:
s=f.read()
ifall(iinsforiinfindstr_list):
files.append(os.path.join(r,fn))
returnfiles
if__name__=='__main__':
files=[iforiinsys.argv[1:]ifnoti.startswith('-')]
unzipfiles=[iforiinfilesifi.endswith('.zip')ori.endswith('.rar')]
xmlfiles=[iforiinfilesifi.endswith('.xml')]
save_unzipdir=Trueif'-s'insys.argvelseFalse
findstr=[i.split('=')[-1]foriinsys.argvifi.startswith('--find=')]
findstring=','.join(['`{}`'.format(i)foriinfindstr])
newdir=extract_files(unzipfiles)
result=findstr_at(newdir,xmlfiles,findstr)
ifnotresult:
msg='Noneofthefile(s)containthegivenstring{}.'
print(msg.format(findstring))
else:
msg='{}file(s)containthegivenstring{}:'
print(msg.format(len(result),findstring))
print(' '.join([i.replace(newdir+os.sep,'')foriinsorted(result)]))
ifnotsave_unzipdir:
shutil.rmtree(newdir)
$python3ziprar.pyaaa.zipaaa2.zipaaa3.zipaaa.xmlaaa1.xmlaaa2.xml--find="Itwas"--find="when"
Noneofthefile(s)containthegivenstring`Itwas`,`when`.
$python3ziprar.pyaaa.zipaaa2.zipaaa3.zipaaa.xmlaaa1.xmlaaa2.xml--find="Itwas"--find="I"
2file(s)containthegivenstring`Itwas`,`I`:
aaa.zip/aaa2.xml
aaa2.zip/aaa2.xml
$python3ziprar.pyaaa.zipaaa2.zipaaa3.zipaaa.xmlaaa1.xmlaaa2.xml--find="Itwas"
2file(s)containthegivenstring`Itwas`:
aaa.zip/aaa2.xml
aaa2.zip/aaa2.xml
❸ 怎么对多个python程序打包
打什么包呢?打成exe的话,可以用一个py2exe的插件,很方便的
❹ PyInstaller 如何打包多个py文件
1、下载pyinstaller
目前pyinstaller支持的python版本为2.3-2.7,可以到官网下载。
2、安装
下载完成后,解压版即权可。
3、pyinstaller使用方法
使用也非常的简单,cmd下进入解压出来的目录,执行如下命令。
python pyinstaller.py [opts] yourprogram.py
主要选项包括:
-F, –onefile 打包成一个exe文件。
-D, –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)。
-c, –console, –nowindowed 使用控制台,无界面(默认)
-w, –windowed, –noconsole 使用窗口,无控制台
❺ 如何将多个python程序打包为exe文件
pyinstaller
❻ 怎么将多个python打包成exe文件
1、安装pywin32,可以参考《怎么给python安装pywin32模块?》,一定要注意对应的python版本,否则不能安装。
❼ python多个文件exe打包时出现的错误
如果是exez运行出错那么:
你用的是python多少?》
打包的是那一个库?
如果是exe闪退那么版:
请在权命令指示符下运行改程序即可,且最好设置关闭框,不然承运很小运行很快的话,运行狂会瞬间关闭造成闪退的样子
❽ 如何如何把多个py文件打包成一个exe文件
python Configure.py #仅第一次执行
python Makespec.py toexe\hello.py #生成的hello.spec文件存默认放在当前目录专的hello文件夹下
python Build.py hello\hello.spec #生成的exe文件在当前目录下的属hello\dist文件夹下
❾ 如何用python setup.py打包多个.py文件到同一个包下面
py_mole是个列表
py_mole=['test','hello'] 就好了