38

I have a Python script that compares existing file names in a folder to a reference table and then determines if it needs to be renamed or not.

As it loops through each filename:

'oldname' = the current file name

'newname' = what it needs to be renamed to

I want rename the file and move it to a new folder "..\renamedfiles"

Can I do the rename and the move at the same time as it iterates through the loop?

4
  • 1
    Show us your loop code - your attempts to solve it. Commented Mar 1, 2017 at 20:49
  • 1
    Renaming and moving a file are the same thing. Commented Mar 1, 2017 at 20:53
  • 1
    Use os.rename and shutil.move Commented Mar 1, 2017 at 20:53
  • 1
    @inspectorG4dget or, just use shutil.move as it does the correct thing anw. Commented Mar 1, 2017 at 20:54

5 Answers 5

52

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')
Sign up to request clarification or add additional context in comments.

Comments

31

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.

2 Comments

As per the docs, shutil.move only copies if the src & dest are on different file systems.
@norman_h: Umm... That's paraphrasing what my answer says? It atomic renames when possible, and falls back to copy-then-delete when they're not on the same file system.
4

Since Python 3.4, working with paths is done easily with pathlib. Moving/renaming a file is done with rename or replace (will unconditionally do the replace). So combining with the parent attribute and the concat operator, you can do:

from pathlib import Path

source = Path("path/to/file/oldname")

target = source.replace(source.parent / "renames" / "newname")

Comments

3

To do both of the operations, you can use the os.rename(src, dest) function.

You should have the wanted directory to save the file in, and the new file name. You can do this for every file you run across in your loop.

For example:

# In Windows
dest_dir = "tmp\\2"
new_name = "bar.txt"
current_file_name = "tmp\\1\\foo.txt"
os.rename(current_file_name, os.path.join(dest_dir, new_name))

The rename function allows you to change the name of the file and it's folder at the same time.

To prevent any errors in renaming and moving of the file, use shutil.move.

Comments

1

Create a Python file in your desired directory and write something like that :

import os

for filename in os.listdir("."):
    if(filename ...):
        newFilename = ...
        os.rename(filename, newFilename)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.