python实现指定文件夹下的指定文件移动到指定位置
作者:lancefate 时间:2023-07-03 08:21:11
本文主要是写了一个将指定文件夹下的指定文件类型移动到指定位置,具体内容如下
# coding:utf-8
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# print os.getcwd()
# 有些文件夹下面有很多文件夹,每个文件夹下面有很多视频文件,现在通过脚本,将文件夹下面的所有文件转移到一个目录下面
# 统计访问的文件夹数量及文件数量
countNum = [0, ]
countFile = [0, ]
# 选择全部移除或者指定后缀名文件
# 查找文件
def move_all_files(dir_path):
if os.path.exists(dir_path):
countNum[0] += 1
# 输出遍历的文件夹数量
print "*****", countNum[0], "*****"+dir_path
# 指定文件夹下的所有文件和文件夹
path_list = os.listdir(dir_path)
# 遍历
for each_path in path_list:
# 如果是文件夹就继续遍历
print each_path
if os.path.isdir(dir_path+"\\"+each_path):
# 移动所有文件到指定目录下面
src=dir_path+"\\"+each_path
move_all_files(src)
else:
# 如果是指定文件类型,则复制文件
file_type = os.path.splitext(each_path)[1]
# 判断是否为选择的文件类型
selected = False
if file_type == select_type or select_type == 'All':
selected = True
if selected:
# 复制文件
src_file = dir_path + "\\" + each_path
des_file = des_pos + "\\" + each_path
print "正在复制", each_path
shutil.copyfile(src_file, des_file)
# 文件+1
countFile[0] += 1
else:
print "指定路径不存在"
# 需要复制文件的文件夹位置
give_pos = r"C:\Users\lance\Downloads\Java Web编程相关"
# 需要复制到的位置
des_pos = r"C:\Users\lance\Downloads\测试"
# All 或者 指定文件后缀名
select_type = 'All'
# 如果不存在,创建
if not os.path.exists(unicode(des_pos, 'utf-8')):
os.mkdir(unicode(des_pos, "utf-8"))
# 移动文件
move_all_files(unicode(give_pos, "utf-8"))
print "将文件从****'", give_pos, "'复制到****'", des_pos, "'"
print "共访问了", countNum[0], "个文件夹"
print "共复制了 ", countFile[0], " 个文件"
来源:https://blog.csdn.net/FEUIFERISFDF/article/details/79382820
标签:python,文件移动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue3 中使用 jsx 开发的详细过程
2024-06-07 16:02:31
![](https://img.aspxhome.com/file/2023/3/121973_0s.png)
Python实现对二维码数据进行压缩
2022-10-22 12:51:59
![](https://img.aspxhome.com/file/2023/2/92172_0s.png)
Python计算机视觉SIFT尺度不变的图像特征变换
2022-08-01 00:28:44
![](https://img.aspxhome.com/file/2023/6/72946_0s.png)
python中的内置函数max()和min()及mas()函数的高级用法
2023-01-21 18:45:43
![](https://img.aspxhome.com/file/2023/6/76096_0s.jpg)
Mysql到Elasticsearch高效实时同步Debezium实现
2024-01-25 15:22:46
![](https://img.aspxhome.com/file/2023/1/93041_0s.jpg)
Golang巧用defer进行错误处理的方法
2023-08-05 03:21:13
golang跳转语句goto,break,continue的使用及区别说明
2024-04-23 09:39:56
css基础教程属性篇之二
2008-07-25 19:23:00
![](https://img.aspxhome.com/file/UploadPic/20087/25/2008725192720609s.jpg)
Python从函数参数类型引出元组实例分析
2022-12-18 01:51:22
Python 如何给图像分类(图像识别模型构建)
2022-07-24 20:34:08
mysql 8.0.12 安装配置图文教程
2024-01-13 20:12:04
![](https://img.aspxhome.com/file/2023/1/129901_0s.jpg)
MySQL 5.5主从同步设置笔记分享
2024-01-27 10:12:29
深入理解ES6 Promise 扩展always方法
2024-04-18 09:46:46
利用Python如何生成hash值示例详解
2022-02-28 23:29:25
Python切片操作去除字符串首尾的空格
2023-08-08 19:19:21
Python面向对象程序设计类的封装与继承用法示例
2022-12-22 08:34:33
举例讲解如何在Python编程中进行迭代和遍历
2023-07-12 04:42:30
pandas时间序列之如何将int转换成datetime格式
2023-03-09 07:50:34
![](https://img.aspxhome.com/file/2023/1/84061_0s.png)
用python 实现在不确定行数情况下多行输入方法
2021-11-08 05:53:27
使用idea创建vue项目的图文教程
2024-05-22 10:42:54
![](https://img.aspxhome.com/file/2023/6/123216_0s.jpg)