如何递归重命名文件夹及其中的文件?

时间:2019-05-11 10:38:29

标签: python-2.7 loops operating-system directory rename

我正在尝试编写一个脚本来递归重命名文件夹及其中的文件。文件夹部分可以工作,但是我必须运行几次才能删除所有特殊字符(无法弄清楚while循环的条件会起作用)。但是,它根本不会重命名文件夹中的文件。甚至不碰他们。不会给出错误或任何提示。不知道为什么。任何帮助表示赞赏。 (我知道,通过查看我的代码,您可以将我识别为新手)

尝试使用while循环,但条件从未满足,因此它无限运行而实际上并未执行任何操作。 for循环将运行,但是我需要它以递归方式运行。

import os
import shutil
import fnmatch

#sets veriables and cwd
os.chdir(r"/Users/Xute/Movies/")
cwd = os.getcwd()
filename = os.listdir()
extensions = ('.png', '.jpg', '.txt', '.nfo')
print(cwd)


#prints folders and filenames, removes special characters, renames all to upper
for foldername, subfolders, filenames in os.walk(cwd):
    if '.HIDDEN' in foldername:
        continue
    if '_' in foldername:
        newfoldername = foldername.replace('_', ' ')
        os.rename(foldername, newfoldername)
    elif '.' in foldername:
        newfoldername = foldername.replace('.', ' ')
        os.rename(foldername, newfoldername)
    elif '-' in foldername:
        newfoldername = foldername.replace('-', ' ')
        os.rename(foldername, newfoldername)
    elif '[' in foldername:
        newfoldername = foldername.replace('[', '')
        os.rename(foldername, newfoldername)
    elif ']' in foldername:
        newfoldername = foldername.replace(']', '')
        os.rename(foldername, newfoldername)
    elif '(' in foldername:
        newfoldername = foldername.replace('(', '')
        os.rename(foldername, newfoldername)
    elif ')' in foldername:
        newfoldername = foldername.replace(')', '')
        os.rename(foldername, newfoldername)
    elif ')' in foldername:
        newfoldername = foldername.replace('  ', ' ')
        os.rename(foldername, newfoldername)
    elif foldername is not cwd:
        os.rename(foldername, foldername.upper())
    else:
        continue
    print('The current folder is ' + foldername)

    #removes txt, image, and info filenames, removes special characters, skips hidden filenames
    for filename in filenames:
        if filename.endswith('.txt') or filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.nfo'):
            os.remove(foldername + r"/" + filename)
        elif filename.startswith('.'):
            continue
        elif os.path.isdir('/Users/Xute/Movies/.hidden'):
            # skip directories
            continue
        elif '_' in filename:
            newfilename = filename.replace('_', ' ')
            os.rename(filename, newfilename)
        #elif '.' in filename:
            #newfilename = filename.replace('.', ' ')
        elif '-' in filename:
            newfilename = filename.replace('-', ' ')
            os.rename(filename, newfilename)
        elif '[' in filename:
            newfilename = filename.replace('[', ' ')
            os.rename(filename, newfilename)
        elif ']' in filename:
            newfilename = filename.replace(']', ' ')
            os.rename(filename, newfilename)
        elif '(' in filename:
            newfilename = filename.replace('(', ' ')
            os.rename(filename, newfilename)
        elif ')' in filename:
            newfilename = filename.replace(')', ' ')
            os.rename(filename, newfilename)
        else:
            continue
        os.rename(filename, filename.upper())
        print(filename)

我希望脚本将所有文件和文件夹重命名为相似的名称(例如,删除一堆特殊字符,使它们都只有标题)

0 个答案:

没有答案