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))
mv
command, python'sshutil.move
has one edge case whereshutil.move
function differs. Go here for full write up. In a nutshell, Python'sshutil.move
will raise an exception (but gnu-coreutilsmv
will not) when your destination is a directory and the directory already has a file with the same name as the source (again for more info see the link provided in the previous sentence). – Trevor Boyd Smithos.system("mv file1 file2")
? – John Strood