Coder Perfect

In Python, how do you relocate a file?


I looked via the Python OS interface but couldn’t find a way to relocate a file. In Python, how would I do the equivalent of $ mv…?

>>> source_files = '/PATH/TO/FOLDER/*'
>>> destination_folder = 'PATH/TO/FOLDER'
>>> # equivalent of $ mv source_files destination_folder

Asked by David542

Solution #1

shutil.move, os.rename(), or os.replace() ()

The syntax is the same in all of them:

import os
import shutil

os.rename("path/to/current/", "path/to/new/destination/for/")
os.replace("path/to/current/", "path/to/new/destination/for/")
shutil.move("path/to/current/", "path/to/new/destination/for/")

The file name ( must be included in both the source and destination arguments. The file will be renamed and transferred if it is altered.

It’s also worth noting that in the first two circumstances, the directory where the new file will be created must already exist. On Windows, a file with that name must not exist or an exception will be thrown, whereas os.replace() will replace a file silently even if this is the case.

In most cases, shutil.move merely calls os.rename, as pointed out by ed in comments on other replies. If the destination is on a different disk than the source, the source file will be copied and subsequently deleted.

Answered by ig0774

Solution #2

Although both os.rename() and shutil.move() rename files, shutil.move() is the most similar to the Unix mv command (). The distinction is that os.rename() fails if the source and destination files are on separate drives, whereas shutil.move() does not care about the disk location of the files.

Answered by Jim Calfas

Solution #3

After Python 3.4, you can also use pathlib’s class Path to move file.

from pathlib import Path


Answered by MoonFruit

Solution #4

You’ll need to import the module for either os.rename or shutil.move. To relocate all of the files, no * character is required.

We have a folder at /opt/awesome called source with one file named awesome.txt.

in /opt/awesome
○ → ls
○ → ls source

>>> source = '/opt/awesome/source'
>>> destination = '/opt/awesome/destination'
>>> import os
>>> os.rename(source, destination)
>>> os.listdir('/opt/awesome')

To verify that the folder name has changed, we used os.listdir. This is the shutil for returning the destination to the source.

>>> import shutil
>>> shutil.move(destination, source)
>>> os.listdir('/opt/awesome/source')

This time, I double-checked that the awesome.txt file I made was still present in the source folder. It is present:)

We’ve now transferred a folder and its contents from a source to a destination and back.

Answered by jmontross

Solution #5

This is what I’m currently using:

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

The system is now fully operational. I hope this information is useful to you.

I made a function that takes a source and destination directory, creates the destination folder if it doesn’t exist, and moves the files. Allows for src file filtering; for example, if you just want to move photographs, you can use the pattern ‘*.jpg,’ which will move everything in the directory by default.

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))

Answered by Peter Vlaar

Post is based on