调用系统的winRAR工具对文件进行压缩和加密
1 import zipfile as zf 2 import platform as pf 3 import subprocess 4 import os 5 import os.path 6 7 rootdir = r'' #设置文件所在目录 8 9 os.chdir(rootdir) #切换到文件所在目录10 11 for parent,dirnames,filenames in os.walk(rootdir):12 for filename in filenames:13 s = filename.split('.')14 target = s[0]+'.zip'15 source = filename16 cmd = ['rar','a','-p%s' % ('123'), target,source]17 #将source文件追加到target压缩文件中,‘a’表示追加,并设置密码为12318 p = subprocess.Popen(cmd,executable=r'C:\Program Files\WinRAR\WinRAR.exe')19 #调用WinRAR程序执行加密压缩操作20 p.wait()