Another way is the βit's better to ask forgiveness than permission" approach, just try renaming it, and if that fails, catch the appropriate OSError and try using the copy method. i.e:
import errno try: os.rename(source, dest): except IOError, ex: if ex.errno == errno.EXDEV:
This has the advantage that it will also work on Windows, where st_dev is always 0 for all partitions.
Please note: if you really want to copy and then delete the source file (i.e., perform the transition), and not just copy, then shutil.move will already do what you want:
Help on function move in module shutil:
move (src, dst)
Recursively move a file or directory to another location.
If the destination is on our current filesystem, then simply use
rename. Otherwise, copy src to the dst and then remove src.
[Edit] Updated due to a comment by Matthew Shinkel, to mention that shutil.move will delete the source code after the copy, which is not necessarily what you need, as the question just mentions copying.
Brian
source share