import os
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的阜新網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
import re
for file in os.listdir(filepath):
if os.isfile(os.path.join(filepath,file)):
newfile = re.sub(name1,name2,file)
os.rename(os.path.join(filepath,file),os.path.join(filepath,newfile))
應(yīng)該能跑吧。。 沒試過
使用rename函數(shù)
1、字典型數(shù)據(jù)類型
例:
d?=?{"星座":"Constellation",?'身高':'Height'}
df.rename(columns=d)
2、函數(shù)的方法
def?f(x):
return?x[0]
df.rename(columns=f)
所用模塊:import os
(一)打開文件夾,獲得舊文件名
path=r"D:\exp\resultdata"? ?//文件夾途徑
for (root, dirs, files) in os.walk(path):
os.walk(path)//遍歷D:\exp\resultdata下文件
(二)獲得新文件名(如何打開一個(gè)文件)
一般新文件名都保存在一個(gè)文件中
new_names_files=open('文件名.txt','r')
content=open('文件名.txt','r')(此時(shí)content是一個(gè)含有所有new name 的列表)
注意:對(duì)新名字(字符串)會(huì)有一些操作
(1)對(duì)文件名(字符)的分割
用split()進(jìn)行分割
a=''wer.werrew/"
(2)list 添加新的元素、
用append()進(jìn)行添加
(3)字典添加新的元素
用update()進(jìn)行添加
實(shí)例:
(三)重命名
利用os.rename()函數(shù)
實(shí)例:舊名字與新名字的數(shù)字相對(duì)
file=['1.max','2.max'……]//舊名字
for file in files:
? ? oldname = os.path.join(root,file)
? ? namesp=file.split('.')
? ? new_namesp=d[int(namesp[0])]//數(shù)字相對(duì)
? ? newname = os.path.join(root,new_namesp)
? ? os.rename(oldname,newname)
文章標(biāo)題:python函數(shù)重命名 python函數(shù)名稱可以隨意命名
瀏覽路徑:http://chinadenli.net/article46/dodeeeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)網(wǎng)站制作、微信公眾號(hào)、網(wǎng)頁設(shè)計(jì)公司、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)