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'] 就好了