开发者

Removing/renaming files in python

开发者 https://www.devze.com 2023-04-06 19:23 出处:网络
I am trying to figure out how I could remove 开发者_JAVA百科certain words from a file name. So if my file name was lolipop-three-fun-sand,i would input three and fun in and they would get removed. Ren

I am trying to figure out how I could remove 开发者_JAVA百科certain words from a file name. So if my file name was lolipop-three-fun-sand,i would input three and fun in and they would get removed. Renaming the file to lolipop--sand. Any ideas on how to start this?


Use string.replace() to remove the words from the filename. Then call os.rename() to perform the rename.

newfilename = filename.replace('three', '').replace('fun', '')
os.rename(filename, newfilename)


import os
line = 'lolipop-three-fun-sand'
delete_list = raw_input("enter ur words to be removed : write them in single quote separated by a comma")
for word in delete_list:
        line = line.replace(word, "")
print line
0

精彩评论

暂无评论...
验证码 换一张
取 消