基于Python——实现解压文件夹中的.zip文件
【背景】当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法——
【代码实现】
#!/usr/bin/env python
#! -*- coding:utf-8 -*- import zipfile
import os
import shutil def un_zip(file_name):
"""解压单个文件"""
zip_file = zipfile.ZipFile(file_name) #读取zip文件
if os.path.isdir(file_name[0:-20]): #判断是否存在文件夹,file_name[0:20]是为了方便我去掉日期和.zip的后缀
pass
else:
os.mkdir(file_name[0:-20]) #创建文件夹 for names in zip_file.namelist(): #解压
zip_file.extract(names,file_name[0:-20])
zip_file.close()
Conf = os.path.join(file_name[0:-20], 'conf') # 删除配置文件,如不需要,可删除
shutil.rmtree(Conf)
if os.path.exists(file_name): #删除zip文件
os.remove(file_name)
print file_name[0:-20],'解压成功' def un_zip_Tree(path): # 解压文件夹中的zip文件
if not os.path.exists(path): # 如果本地文件夹不存在,则创建它
os.makedirs(path)
for file in os.listdir(path): #listdir()返回当前目录下清单列表
Local = os.path.join(path, file) #os.path.join()用于拼接文件路径
if os.path.isdir(file): # 判断是否是文件
if not os.path.exists(Local): #对于文件夹:如果本地不存在,就创建该文件夹
os.makedirs(Local)
un_zip_Tree(path)
else: # 是文件
if os.path.splitext(Local)[1] == '.zip': #os.path.splitext(Remote)[1]获取文件扩展名,判断是否为.zip文件
un_zip(Local) #解压文件
un_zip()函数,可实现对传入的单个.zip文件进行解压放到新文件夹,并将原来的.zip文件删除
un_zip_Tree()函数,可实现对传入的文件夹路径进行处理:找到文件夹里面的.zip文件,交给un_zip()解压
if __name__ == '__main__':
local = raw_input("please input the local path(eg.D:\\V1.6):")
un_zip_Tree(local)
主函数只需要传入需要批量解压的文件夹就可以啦~
基于Python——实现解压文件夹中的.zip文件的更多相关文章
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- 文件夹中含有子文件夹,修改子文件夹中的图像存储格式(python实现)
文件夹中含有子文件夹,修改子文件夹中的图像存储格式,把png图像改为jpg图像,python代码如下: import os import cv2 filePath = 'C:\\Users\\admi ...
- python实现获取文件夹中的最新文件
实现代码如下: #查找某目录中的最新文件import osclass FindNewFile: def find_NewFile(self,path): #获取文件夹中的所有文件 lists = os ...
- python 将指定文件夹中的指定文件放入指定文件夹中
import os import shutil import re #获取指定文件中文件名 def get_filename(filetype): name =[] final_name_list = ...
- 2. 假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实 际数据。
假设当前文件夹中data.csv文件中存放了2020年某饭店营业额,第一列为日期(如2020-02-03),第二列为每天交易额(如3560),文件中第一行为表头,其余行为实 际数据.编写程序,完成下 ...
- PHP遍历、删除文件夹中的所有文件
<?php header("Content-type:text/html;charset=utf-8"); /** * getDirFile 遍历文件夹中的所有文件 * @p ...
- C++读取文件夹中所有的文件或者是特定后缀的文件
由于经常有读取一个文件夹中的很多随机编号的文件,很多时候需要读取某些特定格式的所有文件. 下面的代码可以读取指定文件家中的所有文件和文件夹中格式为jpg的文件 参考: http://www.2cto. ...
- C#遍历指定文件夹中的所有文件(转)
原文链接:http://www.cnblogs.com/qianqianfy/archive/2009/07/08/1518974.html 1. C#遍历指定文件夹中的所有文件 DirectoryI ...
- C#遍历指定文件夹中的所有文件(转)
C#遍历指定文件夹中的所有文件 DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);//遍历文件夹foreach(DirectoryIn ...
随机推荐
- 使用hashlib进行文件校验
import hashlib import os path = r'D:\CentOS 64 位' def file_md5(path): """ 文件校验 :param ...
- JavaScript 模拟 Dictionary
function Dictionary() { var items = {}; //判断是否包含Key值 this.has = function(key) { return key in items; ...
- Web 开发技术文档大全
https://developer.mozilla.org/zh-CN/docs/Web 基础 HTML CSS HTTP 脚本 JavaScript Web API 事件 Web Component ...
- Redis入门到高可用(十七)—— 持久化开发运维常见问题
1.fork操作 2.子进程开销和优化 3.AOF阻塞
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- iPhoneX快速适配,简单到你想哭。
研究了5个小时的iPhoneX适配. 从catalog,storyboard,safearea等一系列文章中发现.如果我们想完全撑满全屏.那直接建一个storyboard就好了.但撑满全屏后,流海就是 ...
- Hbaseflush处理流程
Hbaseflush处理流程 flush的处理是个生产者消费者模式,通过MemStoreFlusher.requestFlush或者requestDelayedFlush来收集请求,通过多个Flush ...
- 【Checkio Exercise】Three Point Circle
计算三角形外接圆的函数: Three Point Circle If we want to build new silos, then we need to make more formal and ...
- SLF4J bindings
see: https://www.slf4j.org/manual.html
- zabbix自动发现自动注册
一.自动发现 1. 2自动注册详细配置 二.自动注册 1. . 2.自动注册详细配置 三 自动安装zabbix客户端脚本 #!/bin/bash #robin path='/etc/zabbix/za ...