国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python遍歷目錄并批量更換文件名和目錄名的方法

2019-11-25 16:33:06
字體:
來源:轉載
供稿:網友

本文實例講述了Python遍歷目錄并批量更換文件名和目錄名的方法。分享給大家供大家參考,具體如下:

#encoding=utf-8#author: walker#date: 2014-03-07#summary: 深度遍歷指定目錄,并將子目錄和文件名改為小寫#注意,此程序只針對windows,windows下文件(夾)名不區分大小寫import osimport os.pathimport shutil#讀入指定目錄并轉換為絕對路徑rootdir = raw_input('root dir:/n')rootdir = os.path.abspath(rootdir)print('absolute root path:/n*** ' + rootdir + ' ***')#先修改文件名for parent, dirnames, filenames in os.walk(rootdir):  for filename in filenames:    pathfile = os.path.join(parent, filename)    pathfileLower = os.path.join(parent, filename.lower())    if pathfile == pathfileLower:  #如果文件名本身就是全小寫      continue    print(pathfile + ' --> ' + pathfileLower)    os.rename(pathfile, pathfileLower)#后修改目錄名,這里注意topdown參數。#topdown決定遍歷的順序,如果topdown為True,則先列舉top下的目錄,然后是目錄的目錄,依次類推;#反之,則先遞歸列舉出最深層的子目錄,然后是其兄弟目錄,然后父目錄。#我們需要先修改深層的子目錄for parent, dirnames, filenames in os.walk(rootdir, topdown=False):  for dirname in dirnames:    pathdir = os.path.join(parent, dirname)    pathdirLower = os.path.join(parent, dirname.lower())    if pathdir == pathdirLower: #如果文件夾名本身就是全小寫      continue    print(pathdir + ' --> ' + pathdirLower)    os.rename(pathdir, pathdirLower)

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python常用遍歷技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西畴县| 香河县| 文水县| 玉林市| 丽水市| 宣武区| 宽甸| 平武县| 凯里市| 临泽县| 虹口区| 安顺市| 沙河市| 咸宁市| 革吉县| 金阳县| 雷波县| 武汉市| 北流市| 兖州市| 龙泉市| 海南省| 平原县| 高雄县| 景东| 昌黎县| 玉林市| 隆昌县| 呼图壁县| 巴马| 肥城市| 永平县| 青田县| 土默特左旗| 洪江市| 平顶山市| 渑池县| 宁乡县| 镶黄旗| 黑水县| 准格尔旗|