Is there any built in functionality in python as to rename files smartly in windows.

I am using os.rename to move files to a higher directory.

The problem is, the directory I move the file to could have a file that has the same name. I want to do what windows allows a user to do in the GUI, which is choose to auto-rename the file to "<file>.<ext>(n)" where n is the smallest integer not used already.

Is there any way to do this built in. If not does anyone have code that does this?


If you are only concerned with functioning in Windows, there is ready possibility, even not built in directly:
Win32 shell: use SHFileOperation:

Finally, the Platinum Option is probably to use the shell functionality which will offer the same facilities as Explorer, including animated icons, the recycle bin and renaming on collision.

commented: Interesting link +3

When I try the code on that page, I get:
from import shell, shellcon
ImportError: No module named

Nvm I got it to run.

Is there a way to make it automatically select "keep both" ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.