欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)指定文件夾下的指定文件移動(dòng)到指定位置

 更新時(shí)間:2018年09月17日 14:31:03   作者:lancefate  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)指定文件夾下的指定文件移動(dòng)到指定位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文主要是寫了一個(gè)將指定文件夾下的指定文件類型移動(dòng)到指定位置,具體內(nèi)容如下

# coding:utf-8
import os
import shutil
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# print os.getcwd()
# 有些文件夾下面有很多文件夾,每個(gè)文件夾下面有很多視頻文件,現(xiàn)在通過腳本,將文件夾下面的所有文件轉(zhuǎn)移到一個(gè)目錄下面

# 統(tǒng)計(jì)訪問的文件夾數(shù)量及文件數(shù)量
countNum = [0, ]
countFile = [0, ]
# 選擇全部移除或者指定后綴名文件


# 查找文件
def move_all_files(dir_path):
 if os.path.exists(dir_path):
  countNum[0] += 1
  # 輸出遍歷的文件夾數(shù)量
  print "*****", countNum[0], "*****"+dir_path
  # 指定文件夾下的所有文件和文件夾
  path_list = os.listdir(dir_path)
  # 遍歷
  for each_path in path_list:
   # 如果是文件夾就繼續(xù)遍歷
   print each_path
   if os.path.isdir(dir_path+"\\"+each_path):
    # 移動(dòng)所有文件到指定目錄下面
    src=dir_path+"\\"+each_path
    move_all_files(src)
   else:
    # 如果是指定文件類型,則復(fù)制文件
    file_type = os.path.splitext(each_path)[1]
    # 判斷是否為選擇的文件類型
    selected = False
    if file_type == select_type or select_type == 'All':
     selected = True
    if selected:
     # 復(fù)制文件
     src_file = dir_path + "\\" + each_path
     des_file = des_pos + "\\" + each_path
     print "正在復(fù)制", each_path
     shutil.copyfile(src_file, des_file)
     # 文件+1
     countFile[0] += 1
 else:
  print "指定路徑不存在"


# 需要復(fù)制文件的文件夾位置
give_pos = r"C:\Users\lance\Downloads\Java Web編程相關(guān)"
# 需要復(fù)制到的位置
des_pos = r"C:\Users\lance\Downloads\測(cè)試"
# All 或者 指定文件后綴名
select_type = 'All'
# 如果不存在,創(chuàng)建
if not os.path.exists(unicode(des_pos, 'utf-8')):
 os.mkdir(unicode(des_pos, "utf-8"))
# 移動(dòng)文件
move_all_files(unicode(give_pos, "utf-8"))
print "將文件從****'", give_pos, "'復(fù)制到****'", des_pos, "'"
print "共訪問了", countNum[0], "個(gè)文件夾"
print "共復(fù)制了 ", countFile[0], " 個(gè)文件"

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論