第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

有沒有辦法在python中刪除部分文件名(路徑)?

有沒有辦法在python中刪除部分文件名(路徑)?

慕勒3428872 2022-10-25 14:56:25
我有大約 50 個(gè)文件,它們的名稱和創(chuàng)建日期分別為 3 次。如何從 python 中的文件名中刪除該部分(您可以展示一個(gè)包含其他數(shù)據(jù)的示例,這并不重要)我嘗試過這樣的事情:file = 'directory/imagehellohellohello.png'keyword = 'hello'if (file.count(keyword) >= 3):    //functionality (here I want to remove the hello's from the file path)
查看完整描述

2 回答

?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

這可以很簡單地使用pathlib

from pathlib import Path


path = Path("directory/imagehellohellohello.png")

target = path.with_name(path.name.replace("hello", ''))


path.rename(target)

這確實(shí)將文件重命名為"directory/image.png".


從 Python 版本 3.8 開始,該rename方法還將新文件的路徑作為Path對象返回。(所以可以這樣做:


target = path.rename(path.with_name(path.name.replace("hello", '')))

使用的方法/屬性:Path.renamePath.with_namePath.name,str.replace


查看完整回答
反對 回復(fù) 2022-10-25
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

file = 'directory/imagehellohellohello.png'

keyword = 'hello'

if keyword*3 in file:

   newname = file.replace(keyword*3, '')

   os.rename(file, newname)


查看完整回答
反對 回復(fù) 2022-10-25
  • 2 回答
  • 0 關(guān)注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號