renaming files in OS X

Ned Deily nad at acm.org
Wed Apr 20 15:43:44 EDT 2011


In article <280CB56A-89B8-4D62-9374-D769B3ACFEBB at semanchuk.com>,
 Philip Semanchuk <philip at semanchuk.com> wrote:
> On Apr 20, 2011, at 10:02 AM, <jyoung79 at kc.rr.com> <jyoung79 at kc.rr.com> 
> wrote:
> > I'm considering using os.rename or shutil for renaming 
> > files on OS X (Snow Leopard).  However, I've read that 
> > shutil doesn't copy the resource fork or metadata for 
> > the files on OS X.  I'm not sure about os.rename though.  
> > I need to keep the resource fork and metadata.  Is it 
> > better if I just use os.system('mv Š') or is os.rename 
> > safe to use?

os.rename() is a simple wrapper around the standard rename system call 
(man 2 rename) so it has the same semantics.  Extended attributes, 
including resource forks, are preserved by rename(2).  Note that the 
system call only works for renames within one file system.  The mv(1) 
program handles cross-system renames by copying and unlinking and the 
Apple-supplied version does copy extended attribute metadata in that 
case.  As documented, none of the shutil copy functions do that.

> I don't know if os.rename() does what you want, but why don't you try a 
> simple test and find out? Surely an empirical test is at least as useful as 
> an answer from someone like me who may or may not know what he's talking 
> about. =)
> 
> The OS X command xattr  shows whether or not a file has extended attributes, 

The 'ls -l' command does as well:

$ ls -l a.jpg
-rw-r--r--@  1 nad  staff  2425268 Apr  4 16:30 a.jpg
$ ls -l@ a.jpg
-rw-r--r--@  1 nad  staff  2425268 Apr  4 16:30 a.jpg
   com.apple.FinderInfo      32

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list