confusion regarding os.path.walk()

Joachim Kaeber joachim.kaeber at imk.fraunhofer.de
Tue Feb 19 05:09:14 EST 2002


Hi,

Andrew Brown wrote:
> The practical question I was left with was "how do you identify broken
> symlinks with python?" I'd have liked a script that rm-ed broken and only
> broken symlinks, and I can't figure out how to test for them.
> os.path.islink(file) will tell me whether it's a symlink. But is there a
> call to say what it's supposed to point at? Then I can test whether that
> exists.

Maybe os.stat is your friend:

% ln -s /dev/null a
% ln -s /dev/xxxx b

Python 2.1 (#1, May 17 2001, 11:31:53)
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> os.stat("/tmp/a")
(8630, 8, 6L, 1, 0, 0, 0, 0, 0, 0)
>>> os.stat("/tmp/b")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: '/tmp/b'

HTH
-- 
Joachim Kaeber                Fraunhofer Institut Medienkommunikation
Schloss Birlinghoven                   phone: +49 (0) 2241 - 14 25 46
D-53754 Sankt Augustin                   fax: +49 (0) 2241 - 14 24 49
Germany                                  http://www.imk.fraunhofer.de



More information about the Python-list mailing list