本文主要是寫了一個將指定文件夾下的指定文件類型移動到指定位置,具體內容如下
# coding:utf-8import osimport shutilimport sysreload(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+"http://"+each_path): # 移動所有文件到指定目錄下面 src=dir_path+"http://"+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 + "http://" + each_path des_file = des_pos + "http://" + 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], " 個文件"
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答