I have a directory full of files, some which have an ampersand in their names. I would like to rename all the files with ampersands and replace each ampersand with a plus (+). I am working with around 10k files. What would be the best method to do this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
import glob, os
for filename in glob.glob(os.path.join(yourPath, "*&*")):
os.rename(filename, filename.replace('&','+'))
回答2:
If you have subdirectories:
import os
for dirpath, dirs, files in os.walk(your_path):
for filename in files:
if '&' in filename:
os.rename(
os.path.join(dirpath, filename),
os.path.join(dirpath, filename.replace('&', '+'))
)
回答3:
import os
directory = '.'
for file in os.listdir(directory):
if '&' in file :
os.rename(file, file.replace('&', '+'))
Replace directory
with your own path.
回答4:
using rename:
$ rename --find '&' --replace '+' *