递归重命名文件和文件夹

时间:2014-06-20 09:43:49

标签: python windows recursion filenames directory

上下文

目前我正在研究大型数字档案。根据法律,这些档案应在未来20至70年左右保存。我的研究是关于如何使用企业搜索解决方案检索这些档案中的文档。在我可以对这些存档执行任何操作之前,应将它们复制到本地存储。大多数档案都通过USB驱动器和手柄接触到我。档案的大小在4GB到250GB之间。与此同时,我正在学习编写python以便能够编写搜索解决方案的脚本。

问题

由于深层嵌套目录和文件,复制文件和目录是有问题的。许多文件或目录都有很长的名称(有时长于Windows仍然会阻塞的255个字符)。

解决方案

编写一个Python脚本,以递归方式从文件夹和文件名中删除所有空格,将这些空格缩短为255个字符以下。同样为了保留旧名称,新名称将是camelCased。

问题

我编写了一个脚本来删除空格和camelCase文件和文件夹名称。但它只适用于所选目录;它不是递归的。我尝试了os.walk函数,但我似乎无法用os.walk实际更改名称。使用os.listdir我可以更改名称,但我不知道如何递归地执行此操作。那么如何用Python递归重命名文件和文件夹呢?

代码

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))

2 个答案:

答案 0 :(得分:0)

我使用python的经验是在Linux上,但也许你可以遍历那个驱动器的全局

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

并使用shutil将文件移动或复制到新位置?

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

两者都非常简单易用,但如果你有使用它们的特定问题,那么关于这两个库的堆栈溢出也有大量的答案。

祝你好运!

答案 1 :(得分:0)

不确定但是使用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))