Rename files, Python/Jython

2019-02-14 06:40发布

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?

4条回答
倾城 Initia
2楼-- · 2019-02-14 06:51
import os
directory = '.'
for file in os.listdir(directory):
    if '&' in file :
        os.rename(file, file.replace('&', '+'))

Replace directory with your own path.

查看更多
forever°为你锁心
3楼-- · 2019-02-14 06:52

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('&', '+'))
            )
查看更多
Deceive 欺骗
4楼-- · 2019-02-14 06:56
import glob, os
for filename in glob.glob(os.path.join(yourPath, "*&*")):
   os.rename(filename, filename.replace('&','+'))
查看更多
该账号已被封号
5楼-- · 2019-02-14 07:14

using rename:

$ rename --find '&' --replace '+' *
查看更多
登录 后发表回答