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
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/file.foo", "path/to/new/destination/for/file.foo") os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo") shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
The file name (file.foo) 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.
Shutil.move, as mentioned in other answers, most of the time simply calls os.rename. If the destination is on a different disk than the source, the source file will be copied and subsequently deleted.
Answered by ig0774
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 disc location of the files.
Answered by Jim Calfas
You can also use pathlib’s class Path to relocate files after Python 3.4.
from pathlib import Path Path("path/to/current/file.foo").rename("path/to/new/destination/for/file.foo")
Answered by MoonFruit
You’ll need to import the module for either os.rename or shutil.move. To relocate all of the files, no * character is required.
At /opt/awesome, we have a source folder with one file entitled awesome.txt.
in /opt/awesome ○ → ls source ○ → ls source awesome.txt python >>> source = '/opt/awesome/source' >>> destination = '/opt/awesome/destination' >>> import os >>> os.rename(source, destination) >>> os.listdir('/opt/awesome') ['destination']
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') ['awesome.txt']
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
This is what I’m currently using:
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)
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. 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))
Answered by Peter Vlaar
Post is based on https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python