您的位置:首页 > 编程语言 > Python开发

解压文件夹中的压缩文件 Python脚本

2009-12-23 19:28 706 查看
下载了很多压缩文件,就写了一个脚本

在Python中使用winrar命令,所以一般压缩文件都支持

有些压缩文件Python中还没有相应的库

你得先把winrar添加到path环境变量中

把代码保存为rar.py

在dos下使用,如:rar.py "D:/A B/C" mkdir

#rar.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=/'mk&del/' to mkdir and delete rar file/n')
exit(0)

#-ibck ,minimized when running
opt=sys.argv[2]
os.chdir(sys.argv[1])
for file in os.listdir('.'):
if os.path.isfile(file) and os.path.splitext(file)[1]=='.rar':
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)
elif opt=='mkdel':
cmd='winrar x -ibck "'+file+'"'+' "'+os.path.splitext(file)[0]+'"//'
os.system(cmd)
os.remove(file)
else :
print('wrong option')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐