使用Python對文件進行批量改名的方法
使用Python對文件進行批量改名
Python在Windows系統(tǒng)下的路徑表示回顧:反斜杠“\”是轉(zhuǎn)義符,如果繼續(xù)用windows習慣使用“\”表示文件路徑,就會產(chǎn)生歧義。
Windows下的原始路徑:C:\Users\LUO\Documents\GitHub\CalculatorT3000\introduction
所以在Python中有三種方法表示:
path="C:\\Users\\LUO\\Documents\\GitHub\\CalculatorT3000\\introduction\\"
path=r'C:\Users\LUO\Documents\GitHub\CalculatorT3000\introduction\'
path='C:/Users/LUO/Documents/GitHub/CalculatorT3000/introduction/'
- 使用斜杠“/”: 'C:/Users/LUO/Documents/GitHub/CalculatorT3000/introduction/'
- 將反斜杠符號轉(zhuǎn)義: "C:\\Users\\LUO\\Documents\\GitHub\\CalculatorT3000\\introduction\\" 因為反斜杠是轉(zhuǎn)義符,所以兩個"\\"就表示一個反斜杠符號
- 使用Python的raw string:r'C:\Users\LUO\Documents\GitHub\CalculatorT3000\introduction\' python下在字符串前面加上字母r,表示后面是一個原始字符串raw string,不過raw string主要是為正則表達式而不是windows路徑設(shè)計的,所以這種做法盡量少用,可能會出問題
使用os 模塊來處理文件和目錄
- python 對文件進行批量改名用到的是 os 模塊中的 listdir 方法和 rename 方法。
- os.listdir(dir) : 獲取指定目錄下的所有子目錄和文件名
- os.rename(原文件名,新文件名) :os.rename(src, dst) 方法用于命名文件或目錄,從 src 到 dst,如果dst是一個存在的目錄, 將拋出OSError
- os.renames() 方法用于遞歸重命名目錄或文件。類似rename()
os.renames(old, new)
old -- 要重命名的目錄
new --文件或目錄的新名字。甚至可以是包含在目錄中的文件,或者完整的目錄樹
- os.getcwd() 返回當前工作目錄
- os.path 模塊主要用于獲取文件的屬性
os.path.basename(path) | 返回文件名 |
os.path.dirname(path) | 返回文件路徑 |
os.path.exists(path) | 如果路徑 path 存在,返回 True;如果路徑 path 不存在,返回 False。 |
os.path.getmtime(path) | 返回最近文件修改時間 |
os.path.getctime(path) | 返回文件 path 創(chuàng)建時間 |
os.path.getsize(path) | 返回文件大小,如果文件不存在就返回錯誤 |
os.path.isfile(path) | 判斷路徑是否為文件 |
os.path.isdir(path) | 判斷路徑是否為目錄 |
os.path.samefile(path1, path2) | 判斷目錄或文件是否相同 |
os.path.sameopenfile(fp1, fp2) | 判斷fp1和fp2是否指向同一文件 |
import os #三種路徑表示方法 #path="C:\\Users\\LUO\\Documents\\GitHub\\CalculatorT3000\\introduction\\" #轉(zhuǎn)義符的方式不能在此使用 #path=r'C:\Users\LUO\Documents\GitHub\CalculatorT3000\introduction\' #path='C:/Users/LUO/Documents/GitHub/CalculatorT3000/introduction/' #從控制臺輸入 path=input("請輸入需要改名的路徑:") #判斷路徑是否存在 if os.path.exists(path): #獲取該目錄下所有文件,存入列表中 fileList=os.listdir(path) n=0 for i in fileList: #設(shè)置舊文件名(就是路徑+文件名) oldname=path+ os.sep + fileList[n] # os.sep添加系統(tǒng)分隔符 #判斷當前是否是文件 if os.path.isfile(oldname): #設(shè)置新文件名 newname=path + os.sep +'calc_'+str(n+1)+'.jpg' os.rename(oldname,newname) #用os模塊中的rename方法對文件改名 print(oldname,'======>',newname) n+=1 else: print('路徑不存在')
補充:使用python批量修改文件名
使用python對文件名進行批量修改
使用split方法對原文件名進行切分,選擇需要的部分進行保留做為新的文件名,也可添加字段。
函數(shù)說明
split()函數(shù)
語法:str.split(str="",num=string.count(str))[n]
參數(shù)說明:
str: 表示為分隔符,默認為空格,但是不能為空(’’)。若字符串中沒有分隔符,則把整個字符串作為列表的一個元素
num:表示分割次數(shù)。如果存在參數(shù)num,則僅分隔成 num+1 個子字符串,并且每一個子字符串可以賦給新的變量
[n]: 表示選取第n個分片
注意:當使用空格作為分隔符時,對于中間為空的項會自動忽略
import os import re def changename(orignname): picture=os.listdir(orignname) for filename in picture: # filename1 = filename.split(".")[0] # filename2=re.findall(r"\d+\.?\d*", filename1)[0]+".png" # srcpath = os.path.join(orignname,filename) # allpath = os.path.join(orignname,filename2) # os.rename(srcpath,allpath) #split("_",2)[1] “_”表示分隔符 ; 2表示分割次數(shù) ; [1]表示選取第 i 個片段 filename1=filename.split("_")[3] #設(shè)置舊文件名(就是路徑+文件名) srcpath=os.path.join(orignname,filename) #設(shè)置新文件名 allpath= os.path.join(orignname,filename1) os.rename(srcpath, allpath) if __name__ == '__main__': orignname=r"D:\AK\GJ\dataset_2\val\labels" changename(orignname)
注意:該方法是直接覆蓋原圖的文件名,不另存,如果想要保留原文件名,請?zhí)崆皬椭?/p>
到此這篇關(guān)于使用Python對文件進行批量改名的方法的文章就介紹到這了,更多相關(guān)Python批量改名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python實現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格
這篇文章主要介紹了基于python實現(xiàn)把json數(shù)據(jù)轉(zhuǎn)換成Excel表格,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05