简体   繁体   English

递归重命名文件和文件夹

[英]Recursively rename files and folders

Context 上下文

At the moment I am working on research about big digital archives. 目前我正在研究大数字档案。 By law, these archives should be preserved for the next 20 to 70 years or so. 根据法律,这些档案应在未来20至70年左右保存。 My research is about how documents from these archives can be retrieved with an enterprise search solution. 我的研究是关于如何使用企业搜索解决方案检索这些档案中的文档。 Before I can do anything with these archives they should be copied to local storage. 在我可以对这些存档执行任何操作之前,应将它们复制到本地存储。 Most archives reach me on USB drives and sticks. 大多数档案都通过USB驱动器和手柄接触到我。 The archives are between 4GB and 250GB in size. 档案的大小在4GB到250GB之间。 In the meantime I am learning to write python to be able to script the search solution. 与此同时,我正在学习编写python以便能够编写搜索解决方案的脚本。

Problem 问题

Copying the files and directories is problematic due to the deep nested directories and files. 由于深层嵌套目录和文件,复制文件和目录是有问题的。 A lot of the files or directories have a pretty long name (sometimes longer than the 255 characters Windows still chokes on). 许多文件或目录都有很长的名称(有时长于Windows仍然会阻塞的255个字符)。

Solution

Writing a Python script which recursively removes all the spaces from the folder and file names to shorten these to something below 255 characters. 编写一个Python脚本,以递归方式从文件夹和文件名中删除所有空格,将这些空格缩短为255个字符以下。 Also to retain the old names the new names will be camelCased. 同样为了保留旧名称,新名称将是camelCased。

Question

I have written a script to remove the spaces and camelCase file and folder names. 我编写了一个脚本来删除空格和camelCase文件和文件夹名称。 But it only works for the chosen directory; 但它只适用于所选目录; it is not recursive. 它不是递归的。 I tried the os.walk function, but I cannot seem to actually change the names with os.walk . 我尝试了os.walk函数,但我似乎无法用os.walk实际更改名称。 With os.listdir I can change the name, but I don't understand how to do it recursively. 使用os.listdir我可以更改名称,但我不明白如何递归。 So how do I rename files and folders recursively with Python? 那么如何用Python递归重命名文件和文件夹呢?

Code

import os

current_path  = os.getcwd() # testing only
print current_path
path = "k:/test3/"
os.chdir(path)
current_path  = os.getcwd()
print current_path # testing only
new_filename= ""
#filenames = os.walk(path, topdown=False)

# all print statusses should be rewritten to lines in log files.

filenames =os.listdir(path)
print filenames
for filename in filenames:
    print "\nOldname: \n" +filename
    new_filename = filename.lower().strip() 
    #make sure all filenames are in lowercase and cut whitespace on both ends.
    if " " in filename: #test for spaces in file or foldername
        fn_parts= [w.capitalize() for w in filename.split()]
        print "The parts are: "
        print  fn_parts
        new_filename="" #empty new_filename after last iteration
        new_filename=new_filename.join(fn_parts)
        print "New filename: \n"+new_filename + "\n"
        os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
    else:
        new_filename=new_filename.title()
        print "New filename: \n"+new_filename + "\n"
        os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

My experience with python is on linux, but perhaps you could iterate through a glob of that drive 我在python上的经验是在Linux上,但也许你可以遍历那个驱动器的全局

https://docs.python.org/2/library/glob.html https://docs.python.org/2/library/glob.html

and use shutil to move or copy the file to a new location? 并使用shutil将文件移动或复制到新位置?

https://docs.python.org/2/library/shutil.html ? https://docs.python.org/2/library/shutil.html

Both are fantastically simple to use, but if you have a specific problem using them, there are also tons of answers already available on stack overflow regarding both these libraries. 两者都非常简单易用,但如果你有一个特定的问题使用它们,那么关于这两个库的堆栈溢出已经有很多答案。

best of luck! 祝你好运!

Not Sure for this But the code changes using os.walk might help you hope so. 不确定但是使用os.walk改变代码可能会帮助你。

import os
current_path  = os.getcwd() # testing only
print current_path
path = "k:/test3/"
os.chdir(path)
current_path  = os.getcwd()
print current_path # testing only
new_filename= ""
#filenames = os.walk(path, topdown=False)

# all print statusses should be rewritten to lines in log files.

filenames =os.listdir(path)
print filenames
for dir,subdir,listfilename in os.walk(path):

    for filename in listfilename:
        print "\nOldname: \n" +filename 
        new_filename = filename.lower().strip() 
        #make sure all filenames are in lowercase and cut whitespace on both ends.
        if " " in filename: #test for spaces in file or foldername
            fn_parts= [w.capitalize() for w in filename.split()]
            print "The parts are: "
            print  fn_parts
            new_filename="" #empty new_filename after last iteration
            new_filename=new_filename.join(fn_parts)
            print "New filename: \n"+new_filename + "\n"
            os.rename(os.path.join(dir, filename), os.path.join(path, new_filename))
        else:
            new_filename=new_filename.title()
            print "New filename: \n"+new_filename + "\n"
            os.rename(os.path.join(path, filename), os.path.join(path, new_filename))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM