python批量处理文件或文件夹


Posted in Python onJuly 28, 2020

本文实例为大家分享了python批量处理文件或文件夹的具体代码,供大家参考,具体内容如下

# -*- coding: utf-8 -*-
import os,shutil
import sys
import numpy as np
##########批量删除不同文件夹下的同名文件夹#############
def arrange_file(dir_path0):
 for dirpath,dirnames,filenames in os.walk(dir_path0):
  if 'my_result' in dirpath:
   # print(dirpath)
   shutil.rmtree(dirpath)


##########批量在不同文件夹下新建同名子文件夹并把文件搬移到子文件夹#############
def arrange_file(dir_path0):
 for dirpath,dirnames,filenames in os.walk(dir_path0):
  for files in filenames:
   total_path = os.path.join(dirpath,files)
   root_path,file_path = total_path.split(dir_path,1)
   if 'png' in file_path:
    new_file_path = '.' + file_path[:-9] + 'new_file_name/'
    # print(file_path)
    # print(new_file_path)
    # print(new_file_path + file_path[-9:])
    # if not os.path.exists(new_file_path):
    #  os.makedirs(new_file_path)
    # shutil.move('.' + file_path,new_file_path + file_path[-9:])

##########批量删除不同文件夹下符合条件的文件##################
def arrange_file(dir_path0):
 for dirpath,dirnames,filenames in os.walk(dir_path0):
  for files in filenames:
   total_path = os.path.join(dirpath,files)
   # print(total_path)
   if 'jpg' in total_path and 'labels' in total_path:
    img = cv2.imread(total_path)
    if np.sum(img) == 0:
     print(total_path)
     os.remove(total_path)

###########批量把文件搬移到上一层文件夹并删除当前文件夹########
def arrange_file(dir_path0):
 for dirpath,dirnames,filenames in os.walk(dir_path0):
  for files in filenames:
   total_path = os.path.join(dirpath,files)
   root_path,file_path = total_path.split(dir_path0,1)
   # print(file_path[:-48])
   # return 0
   if 'jpg' in file_path:
    new_file_path = dir_path0 + file_path[:-48]
    shutil.move(dir_path0 + file_path,new_file_path + file_path[-9:])

 for dirpath,dirnames,filenames in os.walk(dir_path0):
  file_path = dirpath.split('./your_total_path')[1]
  if 'keywords' in file_path:
   # print(dirpath) 
   shutil.rmtree(dirpath)

if __name__=='__main__':
 dir_path0 = './your_total_path'
 arrange_file(dir_path0)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python列表list数组array用法实例解析
Oct 28 Python
Python遍历zip文件输出名称时出现乱码问题的解决方法
Apr 08 Python
浅析Python多线程下的变量问题
Apr 28 Python
Python实现两款计算器功能示例
Dec 19 Python
Python实现判断并移除列表指定位置元素的方法
Apr 13 Python
Python常见字典内建函数用法示例
May 14 Python
浅谈PySpark SQL 相关知识介绍
Jun 14 Python
python 实现快速生成连续、随机字母列表
Nov 28 Python
pytorch 中pad函数toch.nn.functional.pad()的用法
Jan 08 Python
python lambda函数及三个常用的高阶函数
Feb 05 Python
Python2 与Python3的版本区别实例分析
Mar 30 Python
使用Python pip怎么升级pip
Aug 11 Python
在pycharm中显示python画的图方法
Aug 31 #Python
关于django 1.10 CSRF验证失败的解决方法
Aug 31 #Python
详解python中的模块及包导入
Aug 30 #Python
详解Python self 参数
Aug 30 #Python
Python 70行代码实现简单算式计算器解析
Aug 30 #Python
Python类中方法getitem和getattr详解
Aug 30 #Python
Python paramiko模块使用解析(实现ssh)
Aug 30 #Python
You might like
详解php的socket通信
2015/08/11 PHP
PHP实现文件下载【实例分享】
2017/04/28 PHP
javascript之卸载鼠标事件的代码
2007/05/14 Javascript
JavaScript Event学习第十一章 按键的检测
2010/02/10 Javascript
js设置组合快捷键/tabindex功能的方法
2013/11/21 Javascript
Javascript基础知识(三)BOM,DOM总结
2014/09/29 Javascript
jQuery mobile的header和footer在点击屏幕的时候消失的解决办法
2016/07/01 Javascript
jQuery实现简洁的轮播图效果实例
2016/09/07 Javascript
基于Bootstrap框架实现图片切换
2017/03/10 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
webpack引入eslint配置详解
2018/01/22 Javascript
Nodejs 发布自己的npm包并制作成命令行工具的实例讲解
2018/05/15 NodeJs
基于React+Redux的SSR实现方法
2018/07/03 Javascript
es6函数中的作用域实例分析
2020/04/18 Javascript
Vue项目如何引入bootstrap、elementUI、echarts
2020/11/26 Vue.js
在Python中操作字符串之replace()方法的使用
2015/05/19 Python
python机器学习理论与实战(六)支持向量机
2018/01/19 Python
TensorFlow模型保存和提取的方法
2018/03/08 Python
Python分割指定页数的pdf文件方法
2018/10/26 Python
Python实现将通信达.day文件读取为DataFrame
2018/12/22 Python
postman模拟访问具有Session的post请求方法
2019/07/15 Python
Python语法之精妙的十个知识点(装B语法)
2020/01/18 Python
Python使用qrcode二维码库生成二维码方法详解
2020/02/17 Python
python相对企业语言优势在哪
2020/06/12 Python
Python unittest如何生成HTMLTestRunner模块
2020/09/08 Python
CSS3 border-image详解、应用及jQuery插件
2011/08/29 HTML / CSS
CSS3 box-sizing属性详解
2016/11/15 HTML / CSS
CSS3只让背景图片旋转180度的实现示例
2021/03/09 HTML / CSS
大专自我鉴定范文
2013/10/01 职场文书
护士辞职信模板
2014/01/20 职场文书
yy婚礼司仪主持词
2014/03/14 职场文书
毕业班联欢会主持词
2014/03/27 职场文书
房屋出售协议书
2014/04/10 职场文书
学生会辞职信
2015/03/02 职场文书
关于EntityWrapper的in用法
2022/03/22 Java/Android
Golang MatrixOne使用介绍和汇编语法
2022/04/19 Golang