python调用winrar解压_批量文件解压缩脚本(Python3.5 + WinRAR)
import os
s = os.sep
#全局變量
List_Err = []
#dirroot = "D:" + s + "實驗報告" + s #要遍歷的目錄
currentdir = os.path.abspath('.') #遍歷當前目錄
#print(currentdir)
#文件名有空格,則重新命名
for rt, dirs, files in os.walk(currentdir):
for f in files:
fname = os.path.splitext(f)
if fname[1] == ".rar" or fname[1] == ".zip" or fname[1] == ".7z":
if ' ' in fname[0]:
try:
os.rename(currentdir + s + f, currentdir + s +
fname[0].replace(" ","") + fname[1])
except Exception as e:
print('重命名失敗:'+e)
for rt, dirs, files in os.walk(currentdir):
for f in files:
fname = os.path.splitext(f)
if fname[1] == ".rar" or fname[1] == ".zip" or fname[1] == ".7z":
#方法一:手動解壓, 拷貝到cmd下解壓
#rar_command ='"C:\\Program Files\\WinRAR\\WinRAR.exe" x %s * %s\\'%(
# currentdir + s + f, currentdir + s + fname[0])
#方法二:自動解壓, 要求:文件的路徑不能有空格
rar_command ='"C:\\Program Files\\WinRAR\\WinRAR.exe" x %s * %s\\'%(
currentdir + s + f, currentdir + s + fname[0])
#print(rar_command)
if os.system(rar_command) == 0:
print(fname[0] + " == 成功")
else:
List_Err.append(fname[0])
if len(List_Err) > 0:
print(" ============ 失敗名單 ============ ")
for file in List_Err:
print(file)
總結
以上是生活随笔為你收集整理的python调用winrar解压_批量文件解压缩脚本(Python3.5 + WinRAR)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的set和dict_Pyt
- 下一篇: rs232读取智能电表_【技术文章】蜂窝