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)