使用Python实现从各个子文件夹中复制指定文件的方法


Posted in Python onOctober 25, 2018

之前用来整理图片的小程序,拿来备忘,算是使用Python复制文件的一个例子。

# -*- coding: utf-8 -*-
#程序用来拷贝文件并输出图片采集日期等其他信息到Excel中
#文件夹结构:
#2016_07_07
#  -Data_07_07_001
#   -Random1
#    -image001_co.pgm
#    -image001_c1.pgm
#    -image002_co.pgm
#    -image002_c1.pgm
#    -……
#   -Random2
#   -……
#  -Data_07_07_002
#  -Data_07_07_003
#  -……
#所以我们只是拷贝每个子文件夹中,Random1文件夹中的_co.pgm数据
 
import os
import re
import xlwt
 
hang=0
#递归复制文件夹内的文件
def copyFiles(sourceDir,targetDir): 
 global hang   #全局变量,记录即将写入Excel的行号
 worksheet.write(hang, 0, label = sourceDir)
 for file in os.listdir(sourceDir):
  frames = '('+file[file.find('_')+1:]+')' #待写入Excel中的数据
  sourceDir1 = os.path.join(sourceDir,file) #路径名拼接
  targetDir1 = os.path.join(targetDir,file)
  for file in os.listdir(sourceDir1):
   sourceDir2 = os.path.join(sourceDir1,file) 
   #忽略某些特定的子文件夹
   if sourceDir2.find("Random1")>0: 
   #列出源目录文件和文件夹
    count= -1
    for file in os.listdir(sourceDir2): 
    #拼接完整路径
     if re.search('_c0.pgm',file):
      count+=1
      sourceFile = os.path.join(sourceDir2,file) 
      targetFile = os.path.join(targetDir1,file) 
 
      if os.path.isfile(sourceFile):
       if not os.path.exists(targetDir1):
        os.makedirs(targetDir1)
       if not os.path.exists(targetFile) or (os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
        open(targetFile, "wb").write(open(sourceFile, "rb").read())
        print targetFile+" copy succeeded"
    frames = '0-'+str(count)+frames
    worksheet.write(hang, 1, label = 1)
    worksheet.write(hang, 2, label = frames)
    hang+=1
    print frames
 
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Worksheet')
copyFiles("F:/2016_07_07","F:/07_07")
workbook.save('auto_book.xls')
print 'end'

以上这篇使用Python实现从各个子文件夹中复制指定文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用scrapy采集数据时为每个请求随机分配user-agent的方法
Apr 08 Python
Python读取指定目录下指定后缀文件并保存为docx
Apr 23 Python
pycharm远程linux开发和调试代码的方法
Jul 17 Python
详解利用OpenCV提取图像中的矩形区域(PPT屏幕等)
Jul 01 Python
python实现函数极小值
Jul 10 Python
django创建最简单HTML页面跳转方法
Aug 16 Python
python中bytes和str类型的区别
Oct 21 Python
如何在mac环境中用python处理protobuf
Dec 25 Python
Tensorflow实现将标签变为one-hot形式
May 22 Python
python实现mean-shift聚类算法
Jun 10 Python
如何完美的建立一个python项目
Oct 09 Python
python Scrapy框架原理解析
Jan 04 Python
python 实现对文件夹中的图像连续重命名方法
Oct 25 #Python
python微信公众号之关注公众号自动回复
Oct 25 #Python
python 文件查找及内容匹配方法
Oct 25 #Python
利用python循环创建多个文件的方法
Oct 25 #Python
用python生成1000个txt文件的方法
Oct 25 #Python
Python爬虫之正则表达式的使用教程详解
Oct 25 #Python
python实现键盘控制鼠标移动
Nov 27 #Python
You might like
分享微信扫码支付开发遇到问题及解决方案-附Ecshop微信支付插件
2015/08/23 PHP
PHP处理CSV表格文件的常用操作方法总结
2016/07/01 PHP
许愿墙中用到的函数
2006/10/07 Javascript
window.onresize 多次触发的解决方法
2013/11/08 Javascript
jquery.Ajax()方法调用Asp.Net后台的方法解析
2014/02/13 Javascript
JS实现让网页背景图片斜向移动的方法
2015/02/25 Javascript
javascript 小数乘法结果错误的处理方法
2016/07/28 Javascript
js获取隐藏元素的宽高
2017/02/24 Javascript
JavaScript创建对象_动力节点Java学院整理
2017/06/27 Javascript
VeeValidate在vue项目里表单校验应用案例
2018/05/09 Javascript
vue项目使用微信公众号支付总结及遇到的坑
2018/10/23 Javascript
Vue组件Draggable实现拖拽功能
2018/12/01 Javascript
微信小程序和H5页面间相互跳转代码实例
2019/09/19 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
通过实例解析JavaScript for in及for of区别
2020/06/15 Javascript
[03:54]Ehome出征西雅图 回顾2016国际邀请赛晋级之路
2016/08/02 DOTA
[01:27:43]VGJ.S vs TNC Supermajor 败者组 BO3 第三场 6.6
2018/06/07 DOTA
Python内置函数Type()函数一个有趣的用法
2015/02/18 Python
Python的Flask框架的简介和安装方法
2015/11/13 Python
python队列通信:rabbitMQ的使用(实例讲解)
2017/12/22 Python
解决Python2.7中IDLE启动没有反应的问题
2018/11/30 Python
python的turtle库使用详解
2019/05/10 Python
运行tensorflow python程序,限制对GPU和CPU的占用操作
2020/02/06 Python
python集成开发环境配置(pycharm)
2020/02/14 Python
Python3爬虫关于识别检验滑动验证码的实例
2020/07/30 Python
吉尔德利巧克力公司:Ghirardelli Chocolate Company
2019/03/27 全球购物
教师演讲稿范文
2014/01/08 职场文书
《三顾茅庐》教学反思
2014/04/10 职场文书
二手车交易协议书标准版
2014/11/16 职场文书
解除同居协议书
2015/01/29 职场文书
武当山导游词
2015/02/03 职场文书
小学少先队活动总结
2015/05/08 职场文书
夏洛特的网观后感
2015/06/15 职场文书
2019年最新证婚词精选集!
2019/06/28 职场文书
Redis可视化客户端小结
2021/06/10 Redis
常用的MongoDB查询语句的示例代码
2021/07/25 MongoDB