Jhigs Jhigs - 7 months ago 29
Python Question

Python: Renaming first 5 files of a folder

I dont know if this can be done or not, but is there a way that I can rename only the first 5 files in a folder? I know that I can use the os.listdir() or os.walk() to walk through the entire folder, but I only need to rename the first 5 files. I am able to use a Regex to match the files, but the problem is is that there are other files that match the same Regex. Does anyone have any suggestions?
The file name takes the form of "Test Run 1 4-29-2016 2 07 56 PM".

Answer

You can limit the result from listdir:

os.listdir(os.curdir)[:5]