使用Python實(shí)現(xiàn)文件重命名的三種方法
1. 隨機(jī)命名
這個(gè)方法是將文件夾中特定類型文件隨機(jī)命名,下圖是文件原始名稱

隨機(jī)重命名代碼如下:
def fun1(filePath):
"""
重命名函數(shù)fun1
輸入:文件夾路徑
功能:對(duì)文件夾中的全部文件進(jìn)行隨機(jī)命名
"""
suffix = '.txt' # 設(shè)置后綴,篩選特定文件以更改名稱
for file in os.listdir(filePath):
if file.endswith(suffix):
name = file.split('.')[0]
suffix = file.split('.')[1]
salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) # 隨機(jī)輸出8位由英文字符和數(shù)字組成的字符串
newname = name.replace(name, salt)
os.rename(os.path.join(path, file), os.path.join(path, newname + '.' + suffix))
print("End")重命名效果如下:

2. 基礎(chǔ)名+數(shù)字序號(hào)
但是我想將其改成統(tǒng)一的命名,那么就用這種重命名方法
代碼如下:
def fun2(path):
"""
重命名函數(shù)fun2
輸入:文件夾路徑
功能:對(duì)某一個(gè)文件夾中的某一類文件進(jìn)行統(tǒng)一命名,命名格式為:基礎(chǔ)名+數(shù)字序號(hào)
"""
i = 1
suffix = '.txt' # 設(shè)置后綴,篩選特定文件以更改名稱
for file in os.listdir(path):
if file.endswith(suffix):
if os.path.isfile(os.path.join(path, file)):
new_name = file.replace(file, "name_%d" % i + suffix) # 根據(jù)需要設(shè)置基本文件名
os.rename(os.path.join(path, file), os.path.join(path, new_name))
i += 1
print("End")效果如下,可以看到文件被改成了統(tǒng)一名稱 + 數(shù)字序號(hào)的格式,當(dāng)然如果你想修改成別的名稱,只需將"name_%d"這一句中紅字替換為自己的名稱即可。

3. 特定字符替換
假如我的文件中都包含相同的字符(如示例中都包含name),但是我想將其更換為別的字符,那么就用這個(gè)方法。
代碼如下:
def fun3(filePath, old_text, new_text):
"""
重命名函數(shù)fun3
輸入:文件夾路徑、需替換的字符、替換后字符
功能:對(duì)文件名中的特定字符進(jìn)行替換
"""
for i, j, k in os.walk(filePath):
for name in k:
newName = name.replace(old_text, new_text)
name = i + "\\" + name
newName = i + "\\" + newName
os.rename(name, newName)
print("End")效果如下,通過方法3將文件中所有“name”替換為了“名字”

4. 完整代碼
import os
import random
import string
def fun1(filePath):
"""
重命名函數(shù)fun1
輸入:文件夾路徑
功能:對(duì)文件夾中的全部文件進(jìn)行隨機(jī)命名
"""
suffix = '.txt' # 設(shè)置后綴,篩選特定文件以更改名稱
for file in os.listdir(filePath):
if file.endswith(suffix):
name = file.split('.')[0]
suffix = file.split('.')[1]
salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) # 隨機(jī)輸出8位由英文字符和數(shù)字組成的字符串
newname = name.replace(name, salt)
os.rename(os.path.join(path, file), os.path.join(path, newname + '.' + suffix))
print("End")
def fun2(path):
"""
重命名函數(shù)fun2
輸入:文件夾路徑
功能:對(duì)某一個(gè)文件夾中的某一類文件進(jìn)行統(tǒng)一命名,命名格式為:基礎(chǔ)名+數(shù)字序號(hào)
"""
i = 1
suffix = '.txt' # 設(shè)置后綴,篩選特定文件以更改名稱
for file in os.listdir(path):
if file.endswith(suffix):
if os.path.isfile(os.path.join(path, file)):
new_name = file.replace(file, "name_%d" % i + suffix) # 根據(jù)需要設(shè)置基本文件名
os.rename(os.path.join(path, file), os.path.join(path, new_name))
i += 1
print("End")
def fun3(filePath, old_text, new_text):
"""
重命名函數(shù)fun3
輸入:文件夾路徑、需替換的字符、替換字符
功能:對(duì)文件名中的特定字符進(jìn)行替換
"""
for i, j, k in os.walk(filePath):
for name in k:
newName = name.replace(old_text, new_text)
name = i + "\\" + name
newName = i + "\\" + newName
os.rename(name, newName)
print("End")
if __name__ == '__main__':
path = r'E:\pythonProject\utiltools/'
# fun1(path)
# fun2(path)
fun3(path, 'name', '名字')到此這篇關(guān)于使用Python實(shí)現(xiàn)文件重命名的三種方法的文章就介紹到這了,更多相關(guān)Python文件重命名內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬取盤搜的有效鏈接實(shí)現(xiàn)代碼
這篇文章主要介紹了python爬取盤搜的有效鏈接,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
詳解Python如何實(shí)現(xiàn)批量為PDF添加水印
我們有時(shí)候需要把一些機(jī)密文件發(fā)給多個(gè)客戶,為了避免客戶泄露文件,會(huì)在機(jī)密文件中添加水印。本文將利用Python實(shí)現(xiàn)批量為PDF添加水印,需要的可以參考一下2022-05-05
python如何爬取動(dòng)態(tài)網(wǎng)站
在本篇內(nèi)容里小編給各位分享了關(guān)于python如何爬取動(dòng)態(tài)網(wǎng)站的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以參考下。2020-09-09
基于Python實(shí)現(xiàn)一個(gè)簡(jiǎn)易的數(shù)據(jù)管理系統(tǒng)
為了方便的實(shí)現(xiàn)記錄數(shù)據(jù)、修改數(shù)據(jù)沒有精力去做一個(gè)完整的系統(tǒng)去管理數(shù)據(jù)。因此,在python的控制臺(tái)直接實(shí)現(xiàn)一個(gè)簡(jiǎn)易的數(shù)據(jù)管理系統(tǒng),包括數(shù)據(jù)的增刪改查等等。感興趣的可以跟隨小編一起學(xué)習(xí)一下2021-12-12
解決CentOS下ImportError: No module named &a
這篇文章主要介紹了解決CentOS下ImportError: No module named '_sqlite3'的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Python輕松實(shí)現(xiàn)批量郵件自動(dòng)化詳解
在日常工作和生活中,我們經(jīng)常需要發(fā)送郵件,手動(dòng)發(fā)送郵件不僅繁瑣,而且容易出錯(cuò),下面我們就來看看如何使用Python實(shí)現(xiàn)批量郵件自動(dòng)化操作吧2025-02-02

