Python 批量修改檔案名稱並移動檔案到指定目錄

來源:互聯網
上載者:User

標籤:int   讀取   span   +=   utf-8   group   工作   rom   告訴   

# -*- coding: utf-8 -*-import os, sys,re,shutilfrom nt import chdir #讀取中文路徑 u‘‘path=u"D:\\zhyue93\\backup\\個人檔案\\視頻\\10、C#\\4、C# 語言進階特性"dirs=os.listdir(path) #排序 1,2,3...10,11dirs=sorted(dirs,key = lambda i:int(re.match(r‘(\d+)‘,i).group())) #將檔案改名i=10for dir in dirs:    i+=1    newDir=os.path.join(path,dir)    newFiles=os.listdir(newDir)    for file in newFiles:        #rename之前要先用chdir()函數進入到目標檔案所在的路徑,        #告訴python編譯器要重新命名的檔案在哪兒,然後才可以修改        #改變當前工作目錄到指定的路徑        chdir(newDir)        os.rename(file,str(i)+"-"+file)    print ‘-----‘ #將檔案移動到path路徑下print u‘移動檔案‘for dir in dirs:    newDir=os.path.join(path,dir)    newFiles=os.listdir(newDir)    for file in newFiles:        oldFilePath=os.path.join(newDir,file)        newFilePath=os.path.join(path,file)        shutil.move(oldFilePath, newFilePath)#移動檔案到目標路徑

 

Python 批量修改檔案名稱並移動檔案到指定目錄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.