python循环解压rar文件
C:.
│ main.py
│
├─1_STL_算法简介
│ STL_算法简介.rar
│
└─2_STL_算法_填充新值
STL_算法_填充新值.rar
事情是这样的,下载了一套教学视频,文件夹命名规则是:“序号+视频名”,文件夹内是“视频名.rar”,上面选取了其中两个文件作为示例。这样看视频的时候就出现一个问题,每看一次都需要切换一次目录,解压一次文件,烦!
遂写main.py
import rarfile
import shutil
import os
path = os.getcwd()
allFile = os.listdir(path)
for dir in allFile:
if '.' not in dir:
dir2 = os.listdir(path+'\\'+dir)
for file in dir2:
if '.rar' in file:
rar = rarfile.RarFile(path+'\\'+dir+'\\'+file)
rar.extractall(path+'\\'+dir)
dir2 = os.listdir(path+'\\'+dir)
for file in dir2:
if '.wmv' in file:
print(dir)
print(file)
os.chdir(path+'\\'+dir)
os.rename(file, dir+'.wmv')
shutil.copy2(dir+'.wmv', path)
os.chdir(path)
出现问题:
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\rarfile.py", line 2813, in custom_popen
creationflags=creationflags)
File "C:\Python35\lib\subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "C:\Python35\lib\subprocess.py", line 955, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\新建文件夹 (2)\main.py", line 14, in <module>
rar.extractall(path+'\\'+dir)
File "C:\Python35\lib\site-packages\rarfile.py", line 820, in extractall
self._extract(fnlist, path, pwd)
File "C:\Python35\lib\site-packages\rarfile.py", line 885, in _extract
p = custom_popen(cmd)
File "C:\Python35\lib\site-packages\rarfile.py", line 2816, in custom_popen
raise RarCannotExec("Unrar not installed? (rarfile.UNRAR_TOOL=%r)" % UNRAR_TOOL)
rarfile.RarCannotExec: Unrar not installed? (rarfile.UNRAR_TOOL='unrar')
下载UnRar.exe
链接:http://pan.baidu.com/s/1o8ObqCa 密码:dsru
放在当前文件夹,运行main.py。
目录树变为:
C:.
│ 1_STL_算法简介.wmv
│ 2_STL_算法_填充新值.wmv
│ main.py
│ UnRAR.exe
│
├─1_STL_算法简介
│ 1_STL_算法简介.wmv
│ STL_算法简介.rar
│
└─2_STL_算法_填充新值
2_STL_算法_填充新值.wmv
STL_算法_填充新值.rar