简体   繁体   中英

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. 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. The archives are between 4GB and 250GB in size. In the meantime I am learning to write python to be able to script the search solution.

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

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. Also to retain the old names the new names will be camelCased.

Question

I have written a script to remove the spaces and camelCase file and folder names. 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 . With os.listdir I can change the name, but I don't understand how to do it recursively. So how do I rename files and folders recursively with 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

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

and use shutil to move or copy the file to a new location?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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