前段時間碰到一個需要把目錄下文件夾壓縮的項目,但是度娘里沒找到,只好自己寫腳本了。
#coding:utf-8
import os
filePath = raw_input("請輸入路徑:")
if filePath == "":os._exit() #需要退出ds = list(os.walk(filePath)) #獲得所有文件夾的信息列表
fileExe = os.path.realpath(__file__)
x_ipos = fileExe.rfind('\\')
fileExe = fileExe[0:x_ipos]
fileExe += "\\bin\\7z.exe"
print fileExe
for d in ds: #遍歷該列表os.chdir(d[0]) #進入本級路徑print d[0]if d[0] != filePath: #需要過濾目錄自己print ("\'%s\' a -tzip \"%s.zip\" \"%s\"") % (fileExe,d[0],d[0])os.system(("%s a -tzip \"%s.zip\" \"%s\"") % (fileExe,d[0],d[0]))
os.system("pause")
公司規定不能用winrar,并且rar格式的在某些客戶電腦上沒有辦法識別,所以使用了開源軟件7z來進行壓縮,這個段的代碼沒啥東西,主要是執行7z的?壓縮命令 【7z.exe a -tzip?壓縮文件路徑?要壓縮路徑? 】 來進行壓縮。