使用python根據名稱特徵批量移動檔案

2021-10-04 18:42:18 字數 3414 閱讀 6476

1.函式變數

分別為:old_path, fold_feature或者file_feature, target_path

2.函式功能

使用遞迴的方式,把該目錄下【所有】(包含子目錄、子目錄的子目錄)符合該特徵的檔案、資料夾移動到指定路徑

【不可逆!!!不可以使用ctrl+z撤銷,慎重!!!】

import os,re

defchange_foldpath

(old_path, fold_feature, target_path)

:try

:for dirpaths, dirnames, filenames in os.walk(old_path)

:print

(dirpaths,

"\n"

,dirnames,

"\n"

,filenames)

if dirnames:

for anysubdir in dirnames:

if re.search(fold_feature, anysubdir,):

old_subdir_path = old_path +

"/"+ anysubdir

new_subdir_path = target_path +

"/"+ anysubdir

try: os.rename(old_subdir_path, new_subdir_path)

print

("【folderpath】修改folder_path名成功!"

) change_foldpath(new_subdir_path, fold_feature, target_path)

except exception as reason:

print

("【folderpath】修改folder_path出錯!"

)print

("【folderpath】出錯路徑為:\n出錯的folder為:"

.format

(dirpaths,anysubdir)

)print

("【folderpath】出錯原因為:"

,reason)

else

:print

("【folderpath】該資料夾不匹配目標特徵!"

)print

("【folderpath】路徑為:\nfolder為:"

.format

(dirpaths,anysubdir)

) new_subdir_path = old_path +

"/"+ anysubdir

change_foldpath(new_subdir_path, fold_feature, target_path)

else

:print

("【folderpath】路徑中沒有資料夾了!"

.format

(dirpaths)

)except exception as reason:

print

("【folderpath】出錯路徑為:"

,(dirpaths)

)print

("【folderpath】出錯原因為:"

,reason)

defchange_filepath

(old_path, file_feature, target_path)

:try

:for dirpaths, dirnames, filenames in os.walk(old_path)

:# print(dirpaths,"\n",dirnames,"\n",filenames)

if filenames:

for anyfile in filenames:

if anyfile == r'.ds_store'

:pass

else

:if re.search(file_feature, anyfile,):

old_file_path = old_path +

"/"+ anyfile

new_file_path = target_path +

"/"+ anyfile

try: os.rename(old_file_path, new_file_path)

print

("【filepath】更改filename成功!"

)except exception as reason:

print

("【filepath】更改filename出錯!"

)print

("【filepath】出錯路徑為:\n出錯的file為:"

.format

(dirpaths,anyfile)

)print

("【filepath】出錯原因為:"

, reason)

else

:print

("【filepath】該檔案加不匹配目標特徵!"

)print

("【filepath】路徑為:\nfile為:"

.format

(dirpaths, anyfile)

)else

:print

("【filepath】路徑中沒有file了!"

.format

(dirpaths)

)if dirnames:

for anysubdir in dirnames:

subdir_path = old_path +

"/"+ anysubdir

change_filepath(subdir_path, file_feature, target_path)

else

:print

("【filepath】路徑沒有subfolder!"

.format

(dirpaths)

)except exception as reason:

print

("【filepath】出錯路徑為:"

,(dirpaths)

)print

("【filepath】出錯原因為:"

"哈哈"

target_path =

"/users/***x/desktop/***"

change_filepath(old_path, file_feature, target_path)

# change_foldpath(old_path, fold_feature, target_path)

python 根據小說名稱爬取電子書

上一章節小編用python爬取了 斗羅大陸 單本 經過週末馬不停蹄 加班加點 抓耳撓腮的搬磚。終於在今天,經過優化處理後,一款基於python爬蟲來爬取千千 的程式出來了,主要功能有以下幾點 下面,開始展示成果吧,哈哈哈哈 頁數查詢結果顯示 資料夾儲存展示 判斷儲存路徑是否存在,不存在就新建 par...

使用GSon根據節點名稱讀取json陣列

在使用extjs.data.store的writer想servlet提交grid中增刪改過的內容時,會將所有資訊放到乙個以data 可以自定義名稱 下的陣列,這是如果用fromjson去對映成類例項是不行的,要麼將data中的字串讀出來再對映,或者轉為jsonarray,然後讀取每個元素的字元內容,...

python使用knn實現特徵向量分類

這是乙個使用knn把特徵向量進行分類的demo。knn演算法的思想簡單說就是 看輸入的sample點周圍的k個點都屬於哪個類,哪個類的點最多,就把sample歸為哪個類。也就是說,訓練集是一些已經被手動打好標籤的資料,knn會根據你打好的標籤來挖掘同類物件的相似點,從而推算sample的標籤。knn...