python实现复制文件到指定目录
作者:vandance 时间:2022-09-25 20:53:12
这几天在做一个数据集,由于不是很熟悉Linux下的命令,所以特地用了强大的python来做。我之前有一个数据集但是我只要里面名称带有composite和normals的图片,所以找了网上的文章看到了用shutil.copyfile来实现的方法。
# -*- coding: utf-8 -*-
import os,shutil
#shutil.copyfile应用,参数必须具体到文件名
def mycopyfile(srcfile,dstfile):
if not os.path.isfile(srcfile):
print("%s not exit!" % (srcfile))
else:
fpath,fname=os.path.split(dstfile)
if not os.path.exists(fpath):
os.makedirs(fpath)
shutil.copyfile(srcfile,dstfile)
#print("copy %s" % (srcfile,dstfile))
#这个是找到图片的实际地址
def find_filepath(dir1_name,dir2_name,i,file_name=''):
file_path=os.path.join(dir1_name ,dir2_name ,str(i)+file_name+'.png')
return file_path
#这个是我自己的实际应用
if __name__=='__main__':
dir_name=['airplane_test','bottle_test','bunny_test','car_test','motorbike_test','suzanne_test','teapot_test']
obj=['_composite','_normals']
cnt=0
for i in range(7):
direction=dir_name[i]
#print(direction)
for j in range(400):
print(direction,j)
source1=find_filepath('intrinsic',direction,j,obj[0]) #源地址
source2=find_filepath('intrinsic',direction,j,obj[1]) #源地址
dst1=find_filepath('image2norm','testA',cnt) #目标地址
dst2=find_filepath('image2norm','testB',cnt) #目标地址
mycopyfile(source1,dst1)
mycopyfile(source2,dst2)
cnt+=1
值得注意的是,shutil.copyfile(srcfile,dstfile)里面srcfile和dstfile必须是文件名,不能是文件夹。具体应用可以参考上面的main。
如果想要复制文件夹下的全部文件而又不清楚文件的具体数目,可以采用以下os.listdir的方法列出该文件夹下所有文件的名称,
file_path='./resuls'
dirs=os.listdir(file_path)
for dir in dirs:
print(dir)
python常用文件操作汇总
python文件夹操作汇总
python文件操作汇总
来源:https://blog.csdn.net/vandance/article/details/81674348
标签:python,复制文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Navicat for SQLite导入csv中文数据的方法
2024-01-22 18:56:26
![](https://img.aspxhome.com/file/2023/1/110341_0s.jpg)
Python OpenCV图像颜色变换示例
2023-07-30 06:05:00
![](https://img.aspxhome.com/file/2023/5/93505_0s.jpg)
Python OpenCV形态学运算示例详解
2022-03-04 12:23:39
![](https://img.aspxhome.com/file/2023/3/92123_0s.jpg)
vue3.0封装轮播图组件的步骤
2024-05-09 09:30:16
![](https://img.aspxhome.com/file/2023/9/128869_0s.jpg)
python如何将多个模型的ROC曲线绘制在一张图(含图例)
2023-06-24 00:28:54
![](https://img.aspxhome.com/file/2023/5/133535_0s.png)
Mysql 获取表的comment 字段操作
2024-01-19 12:12:28
![](https://img.aspxhome.com/file/2023/0/122870_0s.jpg)
django 将model转换为字典的方法示例
2022-09-16 14:03:09
pandas pd.read_csv()函数中parse_dates()参数的用法说明
2023-07-22 04:51:26
![](https://img.aspxhome.com/file/2023/8/63978_0s.jpg)
Go 语言中 20 个占位符的整理
2024-04-23 09:45:21
Golang中Gin框架的使用入门教程
2024-05-09 09:32:27
python对Excel的读取的示例代码
2023-03-27 17:55:06
![](https://img.aspxhome.com/file/2023/4/112584_0s.png)
python 文件读写和数据清洗
2021-02-10 22:58:43
最新google pr查询接口
2012-03-12 20:00:39
Python3 虚拟开发环境搭建过程(图文详解)
2021-11-19 00:34:51
![](https://img.aspxhome.com/file/2023/2/111872_0s.png)
Python简单遍历字典及删除元素的方法
2021-12-31 08:57:51
Python爬虫进阶Scrapy框架精文讲解
2022-08-21 06:00:16
![](https://img.aspxhome.com/file/2023/3/95833_0s.jpg)
mysql 8.0.16 压缩包安装配置方法图文教程
2024-01-14 13:06:39
![](https://img.aspxhome.com/file/2023/8/119728_0s.jpg)
浅谈如何使用Python控制手机(一)
2023-03-11 14:18:47
![](https://img.aspxhome.com/file/2023/9/68979_0s.jpg)
mysql创建表设置表主键id从1开始自增的解决方案
2024-01-18 13:52:53
![](https://img.aspxhome.com/file/2023/6/67076_0s.png)
Python if else条件语句形式详解
2021-09-21 06:48:24
![](https://img.aspxhome.com/file/2023/5/92535_0s.gif)