symlink overwrite

M

marco

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...

Thanks!
 
J

Jeff Epler

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBCDMcJd01MZaTXX0RAry+AJ9eu66f4Xjw6L3Hpq7SRMKqQdOzZgCfWJTv
5AczIY+fOkynz4QjRSjUQD4=
=HvwM
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top