python实现指定文件夹下的指定文件移动到指定位置

yipeiwu_com7年前Python基础

本文主要是写了一个将指定文件夹下的指定文件类型移动到指定位置,具体内容如下

# 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], " 个文件"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python射线法判断检测点是否位于区域外接矩形内

本文实例为大家分享了python射线法判断点是否位于区域内的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python # -*- coding: utf-8 -...

python中将字典转换成其json字符串

#这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'], 'sub_dic': {...

在Python的while循环中使用else以及循环嵌套的用法

循环使用 else 语句 在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 bre...

python实现傅里叶级数展开的实现

python实现傅里叶级数展开的实现

傅立叶级数的介绍我就不说了,自己也是应用为主,之前一直觉得很难懂,但最近通过自己编程实现了一些函数的傅立叶级数展开之后对傅立叶 级数展开的概念比较清楚了 (1)函数如下 函数图象如...

Python学习资料

官方网站 : www.python.orgPython is an interpreted, interactive, object-oriented programming langu...