之前写了一个自动解压压缩文件到压缩文件所在文件夹的脚本
后根据自己需要,写了另外两个。原理一样
都是使用winrar的命令
第一个脚本没考虑周到,只能解压rar文件
改进后可以支持winrar支持的各种文件
把指定文件夹下的文件保存到指定文件夹
#rardir.py
import os
import sys
src=sys.argv[1]
dst=sys.argv[2]
format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
os.chdir(sys.argv[1])
for file in os.listdir('.'):
if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True:
#cmd='winrar x -ibck "'+file+'" "'+dst+'\\'+os.path.splitext(file)[0]+'\\"'
cmd='winrar x -ibck "'+file+'" "'+dst+'\\"'
os.system(cmd)
os.remove(file)
print('done '+file)
第一个版本的改进
#rardecmp.py
#decompress with winrar
#arguments :filename directory opt
# opt='mkdir' to create directory with the correspond filename
# opt='direct' to decompress rar files in current directory
# opt='mk&del' to mkdir and delete rar file
import os
import sys
if len(sys.argv)!=3:
print ('wrong arguments\n')
print ('rar.py directory opt\n')
print ('opt=\'mkdir\' to create directory with the correspond filename\n')
print ('opt=\'direct\' to decompress rar files in current directory\n')
print ('opt=\'diredel\' to decompress rar files in current directory and delete files\n')
print ('opt=\'mkdel\' to mkdir and delete rar file\n')
exit(0)
#-ibck ,minimized when running
opt=sys.argv[2]
os.chdir(sys.argv[1])
format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
for file in os.listdir('.'):
if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True:
if opt=='mkdir':
cmd='winrar x -ibck "'+file+'"'+' "'+os.path.splitext(file)[0]+'"\\'
os.system(cmd)
elif opt=='direct':
cmd='winrar x -ibck "'+file+'"'
os.system(cmd)
这是一个关于使用Python脚本自动化解压缩多种格式文件的教程。作者分享了两个脚本,分别是`rardir.py`和`rardecmp.py`,它们利用WinRAR命令行接口实现解压缩功能。`rardir.py`可以将指定目录下的各种压缩文件解压到目标目录,而`rardecmp.py`提供了更多选项,如创建解压后的文件夹、直接解压到当前目录等。这两个脚本都支持多种压缩格式,包括rar, zip, 7z等。
6943

被折叠的 条评论
为什么被折叠?



