We were using os.path.exists(). This failed when there was a broken link.
It is better to use os.path.lexists() to check whether something exists before you use that name again for linking to another file. (See also may2011)
$ l foo
-rw-rw-r-- 1 usr grp 8 Jul 11 2012 foo
$ ln -s foo fiBad
create symbolic link `fiBad' to `foo'
$ mv foo foooo
`foo' -> `foooo'
$ ln -s foooo fiGood
create symbolic link `fiGood' to `foooo'
$ l f*
-rw-rw-r-- 1
usr grp
8 Jul 11 2012 foooo
lrwxrwxrwx 1
usr grp
3 Jun 4 18:23 fiBad -> foo
lrwxrwxrwx 1
usr grp
5 Jun 4 18:24 fiGood -> foooo
$ python
Python 2.4.1 (#2, Jul 24 2007, 12:14:31)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-34)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> if os.path.exists("foooo"):
... print "T"
...
T
>>> if os.path.exists("fiGood"):
... print "T"
...
T
>>> if os.path.exists("fiBad"):
... print "T"
... else:
... print "F"
...
F
>>> if os.path.lexists("fiBad"):
... print "T"
... else:
... print "F"
...
T
>>> if os.path.lexists("foooo"):
... print "T"
...
T
>>> if os.path.lexists("fiGood"):
... print "T"
...
T
>>> if os.path.islink("fiGood"):
... print "T"
...
T
>>> if os.path.islink("fiBad"):
... print "T"
... else:
... print "F"
...
T
Useful list of os.path methods here.
No comments:
Post a Comment