I have a set of files开发者_C百科 which the names follow this pattern: xxx - 001, xxx - 002 ..... xxx - 700
What I would like to do it`s a python script which I can invert the order of the name of the files, doing the xxx - 700 to be the xxx - 0001!
Do you mean a Python script to rename all the files?
First, you'll need to do the renaming into a different directory, or as soon as you rename "xxx - 700" to "xxx - 001", it will overwrite the existing file "xxx - 001". So have it rename "xxx - 700" to "temp/xxx - 001", and "xxx - 699" to "temp/xxx - 699", etc. Then manually move everything from temp back to the current directory.
The script should use os.rename to rename each file.
Now, just have the script use os.listdir to find all the files in the directory. Perhaps find the one with the highest number and ensure that the directory contains files with all numbers from 001 to the highest (or at some point there will be an error).
Now iterate over all numbers i from 001 to the highest, and rename each file from "xxx - i" to "xxx - (highest - i)".
You should iterate through the directory using the glob
module, then using regex
find the two groups in the file name and then you can do a shutil.move
to rename the files.
精彩评论