使用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写的ARP攻击代码实例
Jun 04 Python
python中self原理实例分析
Apr 30 Python
使用Python简单的实现树莓派的WEB控制
Feb 18 Python
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
Feb 03 Python
Python对多属性的重复数据去重实例
Apr 18 Python
Django组件之cookie与session的使用方法
Jan 10 Python
Python基础教程之if判断,while循环,循环嵌套
Apr 25 Python
python 含子图的gif生成时内存溢出的方法
Jul 07 Python
opencv python如何实现图像二值化
Feb 03 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
Mar 03 Python
python excel多行合并的方法
Dec 09 Python
总结Python连接CS2000的详细步骤
Jun 23 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
php 设计模式之 工厂模式
2008/12/19 PHP
php 无限级数据JSON格式及JS解析
2010/07/17 PHP
php强制下载类型的实现代码
2011/04/21 PHP
php站内搜索并高亮显示关键字的实现代码
2011/12/29 PHP
smarty基础之拼接字符串的详解
2013/06/18 PHP
PHP间隔一段时间执行代码的方法
2014/12/02 PHP
javascript在myeclipse中报错的解决方法
2013/10/29 Javascript
js和jquery使按钮失效为不可用状态的方法
2014/01/26 Javascript
JS在IE下缺少标识符的错误
2014/07/23 Javascript
node.js 和HTML5开发本地桌面应用程序
2016/12/13 Javascript
微信小程序 css使用技巧总结
2017/01/09 Javascript
微信小程序 label 组件详解及简单实例
2017/01/10 Javascript
js随机生成一个验证码
2017/06/01 Javascript
详解微信第三方小程序代开发
2017/06/23 Javascript
JavaScript实现简单图片轮播效果
2017/08/21 Javascript
vue与bootstrap实现时间选择器的示例代码
2017/08/26 Javascript
使用vue官方提供的模板vue-cli搭建一个helloWorld案例分析
2018/01/16 Javascript
详解@Vue/Cli 3 Invalid Host header 错误解决办法
2019/01/02 Javascript
小程序实现按下录音松开识别语音
2019/11/22 Javascript
vue-router定义元信息meta操作
2020/12/07 Vue.js
Python通过Django实现用户注册和邮箱验证功能代码
2017/12/11 Python
python微信跳一跳系列之棋子定位像素遍历
2018/02/26 Python
Python unittest模块用法实例分析
2018/05/25 Python
TensorFlow基于MNIST数据集实现车牌识别(初步演示版)
2019/08/05 Python
Pycharm使用远程linux服务器conda/python环境在本地运行的方法(图解))
2019/12/09 Python
Pytorch基本变量类型FloatTensor与Variable用法
2020/01/08 Python
Python for循环搭配else常见问题解决
2020/02/11 Python
香港迪士尼乐园酒店预订:Hong Kong Disneyland Hotels
2017/05/02 全球购物
Artist Guitars新西兰:乐器在线商店
2017/09/17 全球购物
大学生毕业的自我鉴定
2013/11/13 职场文书
毕业生自我鉴定
2013/12/04 职场文书
医生进修自我鉴定
2014/01/19 职场文书
小学校长先进事迹材料
2014/05/13 职场文书
幼儿园教师自荐书
2015/03/06 职场文书
英语通知范文
2015/04/22 职场文书
护士爱岗敬业心得体会
2016/01/25 职场文书