symlink overwrite

Jeff Epler jepler at unpythonic.net
Wed Jul 28 19:13:33 EDT 2004


On Wed, Jul 28, 2004 at 06:51:15PM -0400, marco wrote:
> 
> Hi all,
> 
> I know about os.symlink(src, dst), but is there
> a way to overwrite an existing symlink e.g. "ln -sf"?
> Short of checking and, if necessary, having to delete
> the existing link by hand...

That's what gnu ln does for "ln -sf", though for good measure it stat()s
three times!

$ strace -e stat64,symlink,unlink  ln -sf a b
stat64("b", 0xfeee1290) = -1 ENOENT (No such file or directory)
symlink("a", "b")       = 0

$ strace -e stat64,symlink,unlink  ln -sf a b
stat64("b", 0xfeef69e0) = -1 ENOENT (No such file or directory)
stat64("b", 0xfeef68d0) = -1 ENOENT (No such file or directory)
stat64("a", 0xfeef69d0) = -1 ENOENT (No such file or directory)
unlink("b")             = 0
symlink("a", "b")       = 0

A single os.lstat() should do nicely in your case, or a catch around the
unlink.

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040728/8f86b89a/attachment.sig>


More information about the Python-list mailing list