1.根据自己需求写路径,亲测可用。
2.只能解压zip和rar,其他未测试
3.会解压规定目录及其子目录下所有的压缩包。
4.步骤是将压缩包先保存到别的地方,再解压,再删除。
5.解压rar的时候,底层调用的unrar,需要本地安装winrar,或者将unrar的程序放到当前目录(*重点*)。
6.在什么服务器运行,在那个服务器打包最好(踩坑后的感悟)
#!/usr/local/python
# -*- coding: utf-8 -*-
import os
import shutil
import time
import zipfile
import rarfile
#判断是不是zip
def scan_zip(path):
for f in os.listdir(path): #由于这里是当前路径,所以需要把这个代码文件和你要处理的文件放到同一个文件夹里
if f.endswith('.zip'):
return f
#判断是不是rar
def scan_rar(path):
for f in os.listdir(path): #由于这里是当前路径,所以需要把这个代码文件和你要处理的文件放到同一个文件夹里
if f.endswith('.rar'):
return f
#解压zip
def unzip_it(f,p):
z=zipfile.ZipFile(f,'r')
z.extractall(p)
z.close()
#解压rar
def unrar_it(f,p):
rar=rarfile.RarFile(f,'r')
rar.extractall(p)
rar.close()
#删除文件
def delete(f):
os.remove(f)
time.sleep(3)
#获取所有文件夹
def get_filelist(dir,Filelist):
newDir = dir
if os.path.isfile(dir):
Filelist.append(dir)
elif os.path.isdir(dir):
for s in os.listdir(dir):
newDir = os.path.join(dir,s)
get_filelist(newDir,Filelist)
return Filelist
#读取文件路径
def get_file_add():
with open('路径.txt','r') as f:
return f.read()
#执行总命令
if __name__ == '__main__':
print('解压文件的程序,别关,受累。。。。')
path = get_file_add()
while True:
list = get_filelist(path,[])
for f in list:
folder_name = f.split('\\')
new_name = ''
new_name1 = ''
del(folder_name[-1])
for i in folder_name:
new_name = new_name + i + '\\'
new_name1 = new_name[0:-1]
zip_file = scan_zip(new_name1)
if zip_file:
shutil.copy(new_name + zip_file, '已解压的文件')
unzip_it(new_name + zip_file,new_name1)
delete(new_name + zip_file)
time.sleep(20)
rar_file = scan_rar(new_name1)
if rar_file:
shutil.copy(new_name + rar_file, '已解压的文件')
unrar_it(new_name + rar_file,new_name1)
delete(new_name + rar_file)
time.sleep(20)
time.sleep(120)