使用Python對文件進行批量改名的方法
使用Python對文件進行批量改名
Python在Windows系統(tǒng)下的路徑表示回顧:反斜杠“\”是轉義符,如果繼續(xù)用windows習慣使用“\”表示文件路徑,就會產生歧義。
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/'
- 將反斜杠符號轉義: "C:\\Users\\LUO\\Documents\\GitHub\\CalculatorT3000\\introduction\\" 因為反斜杠是轉義符,所以兩個"\\"就表示一個反斜杠符號
- 使用Python的raw string:r'C:\Users\LUO\Documents\GitHub\CalculatorT3000\introduction\' python下在字符串前面加上字母r,表示后面是一個原始字符串raw string,不過raw string主要是為正則表達式而不是windows路徑設計的,所以這種做法盡量少用,可能會出問題
使用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\\"
#轉義符的方式不能在此使用
#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:
#設置舊文件名(就是路徑+文件名)
oldname=path+ os.sep + fileList[n] # os.sep添加系統(tǒng)分隔符
#判斷當前是否是文件
if os.path.isfile(oldname):
#設置新文件名
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]
#設置舊文件名(就是路徑+文件名)
srcpath=os.path.join(orignname,filename)
#設置新文件名
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>
到此這篇關于使用Python對文件進行批量改名的方法的文章就介紹到這了,更多相關Python批量改名內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)股票歷史數(shù)據(jù)可視化分析案例
股票交易數(shù)據(jù)分析可直觀股市走向,對于如何把握股票行情,快速解讀股票交易數(shù)據(jù)有不可替代的作用,感興趣的可以了解一下2021-06-06
ubuntu20.04運用startup application開機自啟動python程序的腳本寫法
這篇文章主要介紹了ubuntu20.04運用startup application開機自啟動python程序的腳本寫法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10
scrapy結合selenium解析動態(tài)頁面的實現(xiàn)
這篇文章主要介紹了scrapy結合selenium解析動態(tài)頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Tensorflow中的降維函數(shù)tf.reduce_*使用總結
這篇文章主要介紹了Tensorflow中的降維函數(shù)tf.reduce_*使用總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
解決pip install xxx報錯SyntaxError: invalid syntax的問題
今天小編就為大家分享一篇解決pip install xxx報錯SyntaxError: invalid syntax的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
pandas中read_sql使用參數(shù)進行數(shù)據(jù)查詢的實現(xiàn)
本文主要介紹了pandas中read_sql使用參數(shù)進行數(shù)據(jù)查詢的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06

