Skip to content

Instantly share code, notes, and snippets.

@badmonkey7
Created November 14, 2020 11:00
Show Gist options
  • Select an option

  • Save badmonkey7/e19ac6e24c8c7412ddc76305997982ce to your computer and use it in GitHub Desktop.

Select an option

Save badmonkey7/e19ac6e24c8c7412ddc76305997982ce to your computer and use it in GitHub Desktop.
提取当前目录下所有的压缩文件到对应的文件夹下
#! /usr/bin/python
'''
@author: badmonkey
@software: PyCharm
@file: extract.py.py
@time: 2020/11/14 下午6:31
'''
import os
# 获取当前路径
pwd = os.getcwd()
#获取当前路径下的所有文件名
filenames = os.listdir(pwd)
# 分类 zip 和 tar.gz
tar = []
zip = []
for i in filenames:
if i.endswith('zip'):
zip.append(i)
elif i.endswith("tar.gz"):
tar.append(i)
for filename in tar:
prefix = filename[:filename.index('.')]
cmd = "mkdir {} && tar -xvzf {} -C {}/".format(prefix,filename,prefix)
try:
os.system(cmd)
os.system("rm -rf {}".format(filename))
except Exception as e:
print("fail to extract {}".format(filename))
continue
for filename in zip:
prefix = filename[:filename.index('.')]
cmd = "mkdir {} && unzip {} -d {}/".format(prefix,filename,prefix)
try:
os.system(cmd)
os.system("rm -rf {}".format(filename))
except Exception as e:
print("fail to extract {}".format(filename))
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment