January 04, 2005

A Mac friendly python move?

I had a need to find a MacOS (actually HFS) friendly alternative to shutil.move.

I have some some code in a utility which shovels '.webloc' files around based on modification dates and works well even on files with resource forks but will fail silently (eating the resource fork) if the move crosses partitions (or worse, moves to a file system like UFS or NFS). It's had this disclaimer in there for a few days and I wanted to make it go away:

# this will break across file systems so I need a better way...
shutil.move( aFile, newDir + fileName )

Ideally, it would be something I could dynamically import and if it fails, fall back to using shutil.move. So I had a few things to learn.

The macostools module doesn't have a move, but it does have a copy, which'll have to do (for now). The big question... is it always installed? I've no idea, but I'm going to have to find a generic install of Panther to find out.

Here's my interim solution, such as it is:

def moveFile( srcPath, dstPath ) :
    try :
        import macostools
        macostools.copy( srcPath, dstPath )
        os.remove( srcPath )

    except ImportError :
        import shutil
        shutil.move( srcPath, dstPath )

In a perfect world, I would be able to hand this off to a module which would do all the consideration about the partition and pick the best mechanism (like various bits of Jim Luther's MoreFiles collection), but this will do for now.

Posted by Dave at January 4, 2005 11:13 PM
Comments