Rename and move file with Python

Yes you can do this. In Python you can use the move function in shutil library to achieve this.

Let's say on Linux, you have a file in /home/user/Downloads folder named "test.txt" and you want to move it to /home/user/Documents and also change the name to "useful_name.txt". You can do both things in the same line of code:

import shutil

shutil.move('/home/user/Downloads/test.txt', '/home/user/Documents/useful_name.txt')

In your case you can do this:

import shutil

shutil.move('oldname', 'renamedfiles/newname')

os.rename (and os.replace) won't work if the source and target locations are on different partitions/drives/devices. If that's the case, you need to use shutil.move, which will use atomic renaming if possible, and fallback to copy-then-delete if the destination is not on the same file system. It's perfectly happy to both move and rename in the same operation; the operation is the same regardless.