本文摘自php中文网,作者不言,侵删。
下面为大家分享一篇python复制文件到指定目录的实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧周末出去爬山,照了一大堆照片回来,照片同时存储为jpg和DNG格式,我用adobe bridge将dng格式的照片中要保留的筛选出来后,就不想再对着一张张去挑jpg的照片了,于是用python写个小程序帮我挑,代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import os
import shutil
targetnames = os.listdir( 'D:\\Pictures\\照片\\2016年\\东灵山\\star' )
filenames = os.listdir( 'D:\\Pictures\\照片\\2016年\\东灵山\\jpg' )
flag=[]
for name in targetnames:
if '.DNG' ==name[-4:]:
targetnames[targetnames.index(name)]=name[:-4]
flag.append(True)
else :
flag.append(False)
continue
for name in targetnames:
if flag[targetnames.index(name)]:
for sname in filenames:
if '.JPG' ==sname[-4:]:
if name==sname[:-4]:
shutil.copyfile( 'D:\\Pictures\\照片\\2016年\\东灵山\\jpg\\' +sname, 'D:\\Pictures\\照片\\2016年\\东灵山\\fabu\\' +sname)
|
使用了os和shutil两个模块,os.listdir用于读取目标目录中的文件名称,star文件夹中存储了我手动筛选出来的DNG格式图片,jpg文件夹中存储了所有的jpg格式图片,于是在获取到所有DNG格式图片后使用flag数组标记一下,然后循环遍历targetnames数组寻找对应名称的jpg文件,找到的话就使用shutil.copyfile复制到指定文件夹,然后就大功告成了!
相关推荐:
python获取程序执行文件路径的方法
Python3.5 创建文件的简单实例
以上就是python复制文件到指定目录的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python建立文件怎么弄
Python可以做嵌入式吗
Python注释符是什么意思
Python在函数中使用列表作为默认参数的介绍(代码示例)
Python全栈工程师需要学什么
Python中几种常用字符串函数
Python如何去除字符串中不想要的字符
笔记之 Python正则表达式
二进制数1001001转换成十进制数等于多少
Python中有关filter的用法详解
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python复制文件到指定目录