Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move files between mounts using Python?

I've been using os.rename to move files in Python. However, it appears that this fails if you try to move files between one mountpoint and another (on Linux).

Is there a Python library or function to move files between mountpoints, similar to mv on Linux?

like image 999
Andrew Ferrier Avatar asked Dec 15 '22 18:12

Andrew Ferrier


2 Answers

This is a common caveat with os.rename. The higher level shutil module and it's move method works around this for you by using os.rename() or shutil.copy2() as appropriate.

One thing to keep in mind is that this loses the atomic guarantee that os.rename has if it isn't on the same filesystem.

See https://docs.python.org/2/library/shutil.html#shutil.move

like image 78
TkTech Avatar answered Dec 29 '22 14:12

TkTech


You can use mv using subprocess

from subprocess import check_call

check_call(["mv","src","dest"])
like image 36
Padraic Cunningham Avatar answered Dec 29 '22 14:12

Padraic Cunningham