Removing/renaming files in python
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
精彩评论