python怎么解压rar文件_用Python解压缩rar、zip文件的方法

本文介绍了使用Python解压RAR和ZIP文件的方法,通过示例代码展示了如何使用winrar命令进行解压缩,并提供了不同选项的用法,如创建目录、直接解压以及解压并删除源文件。
摘要由CSDN通过智能技术生成

玩蛇网本文为大家提供关于用Python解压缩rar、zip文件的方法源码。Python语言对文件方面的处理还是很方便的,例如以前有为大家介绍过Python读取分割压缩TXT文本文件的方法、Python批量删除windows下svn文件夹的方法等,都是很实用的文件相关操作方法。python脚本如何解压指定路径下的所有rar文件?

5892e4e9a827f556e4f995e6dee331fd.png

下边就来看下用Python解压缩rar、zip文件的方法源码:

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)

#www.iplaypy.com第一个版本的改进

#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)

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/text-file/tf2643.html

相关文章 Recommend

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值