Can python move files to another directory?

This is what I'm using at the moment:

import os, shutil
path = "/volume1/Users/Transfer/"
moveto = "/volume1/Users/Drive_Transfer/"
files = os.listdir(path)
files.sort()
for f in files:
    src = path+f
    dst = moveto+f
    shutil.move(src,dst)

Now fully functional. Hope this helps you.

Edit:

I've turned this into a function, that accepts a source and destination directory, making the destination folder if it doesn't exist, and moves the files. Also allows for filtering of the src files, for example if you only want to move images, then you use the pattern '*.jpg', by default, it moves everything in the directory

import os, shutil, pathlib, fnmatch

def move_dir(src: str, dst: str, pattern: str = '*'):
    if not os.path.isdir(dst):
        pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
    for f in fnmatch.filter(os.listdir(src), pattern):
        shutil.move(os.path.join(src, f), os.path.join(dst, f))

You may use the following template to move a file in Python:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Alternatively, you may use this template to move a directory:

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

Let’s now review some examples with the steps to move your file or directory in Python.

Step 1: Capture the Original Path

To begin, capture the original path where your file is currently stored.

For example, let’s suppose that a CSV file is stored in a folder called Test_1:

C:\Users\Ron\Desktop\Test_1\my_csv_file.csv

Where the file name is ‘my_csv_file’ and the file extension is csv.

Step 2: Capture the Target Path

Next, capture the target path where the file will be moved.

For our example, let’s move the CSV file to a folder called Test_2:

C:\Users\Ron\Desktop\Test_2\my_csv_file.csv

Step 3: Move the File using Python

You may now utilize this template to move the file to the target location:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be moved\file name.file extension'

shutil.move(original, target)

Make sure to place the ‘r‘ character before each of your paths to avoid the following error:

‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

For our example, the code to move the CSV file from the original location (i.e., Test_1) to the target location (i.e., Test_2) is as follows:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_csv_file.csv'
target = r'C:\Users\Ron\Desktop\Test_2\my_csv_file.csv'

shutil.move(original, target)

Once you run the code in Python (adjusted to your paths), the CSV file will be moved to the Test_2 folder.

Rename the File when Moving it

Alternatively, you can rename your file when you move it to your target location.

For instance, let’s suppose that a new JPG file is stored in the Test_1 folder (where the file name is image).

The code below can then be used to move the file (with the original file name of ‘image‘) to the target location with a new file name (‘new_image‘):

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r'C:\Users\Ron\Desktop\Test_2\new_image.jpg'

shutil.move(original, target)

The file, with the new name, should now appear in the Test_2 folder.

Move a Directory using Python

So far, you have seen how to move a file in Python.

Alternatively, you may move a directory using this template (without specifying any file extension):

import shutil

original = r'original path where the directory is currently stored\directory name'
target = r'target path where the directory will be moved\directory name'

shutil.move(original, target)

For instance, let’s say that a new directory was added to the Test_1 location, where the directory name is my_folder.

Therefore, the following code can be used to move the directory to the Test_2 target location:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\my_folder'
target = r'C:\Users\Ron\Desktop\Test_2\my_folder'

shutil.move(original, target)

The directory would now appear under the target location.

You just saw how to move a file in Python using shutil.move. You may also want to check the following guide that explains how to copy a file in Python.

How do I move a file to another directory in Python?

Steps to Move a File in Python.
Find the path of a file. We can move a file using both relative path and absolute path. ... .
Use the shutil.move() function. The shutil. ... .
Use the os.listdir() and shutil move() function to move all files. Suppose you want to move all/multiple files from one directory to another, then use the os..

Can you move files with Python?

Python provides functionality to move files or directories from one location to another location. This can be achieved using shutil. move() function from shutil module.

How do I move a file from one directory to another directly?

Use the mv command to move a file from one location to another. To move a file on a computer with a graphical interface, you open the folder where the file is currently located, and then open another window to the folder you want to move the file into. Finally, you drag and drop the file from one to the other.

How do I move a PDF from one directory to another in python?

move() method move Files in Python using the. The shutil. move() method takes two arguments first one is the complete source path and the second one is the destination path (including the file/folder name to move), the move function will move the file from source to the destination.