confusion regarding os.path.walk()
Andrew Brown
killspam at darwinwars.com
Tue Feb 19 05:00:26 EST 2002
Steven Majewski <sdm7g at Virginia.EDU> wrote in
news:mailman.1014078032.30409.python-list at python.org:
>
> You need to os.path.join() the dirname to the filename and do your
> test on that. The filenames are relative to the directory arg,
> not to the process current directory.
>
I had a similar problem yesterday: I wanted to rename all the files on my
site consistently (no spaces, no uppercase, all to end .html) and then --
so as not to break search engines -- to symlink the new names to the old
names. This will let me slowly fade out the old names.
Part 1 went fine, and left me with a text file recording the changes,
which could then be reconstructed as a dictionary of file==>symlink
needed pairs. But when I came to do the second part, with os.path.walk, I
found that all the calls to make symlinks produced broken links with or
without os.path.join(). I had to put into the visitor function a call to
os.chdir(os.curdir) and then make the symlink with os.system
(linkcommand). ("linkcommand" was a previously constructed string like
'ln-s newname oldname')
I found later that I could have used os.symlink() instead of the call to
os.system() but I'm still puzzled that you can't apparently create
symlinks in a directory that's a subdirectory relative to where you are.
Is this a unix/linux feature or a python one?
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.
More information about the Python-list
mailing list