confusion regarding os.path.walk()

Robin Munn rmunn at pobox.com
Tue Feb 19 15:00:27 EST 2002


On Tue, 19 Feb 2002 18:21:14 GMT, Andrew Brown <killspam at darwinwars.com> wrote:
>
>> I think perhaps you both are somehow thinking that os.path.walk
>> changes it's current directory as it 'walks' the tree: it doesn't.
>> 
>Tuesday, February 19, 2002, 18:02:08, you wrote:
>
>S> The current working directory stays the same and all relative
>S> paths are relative to it. When os.path.walk gives you a directory
>S> and a list of files in that directory, unless that directory is
>S> your current working directory, you need to either join the paths
>S> or change your working directory to access the files.
>
>
>Thanks for your help. What you describe is what I thought should
>happen. But I couldn't figure out how to join the paths. It always
>looked right: I'd do debugging runs that simply printed out the
>command it was supposed to execute, and I could get results like
>
>'ln -s ./wombats/habitat/burrows.html ./wombats/habitat/Burrows.htm'
>
>(which I think should work) but when I ran this through os.system(),
>it failed every time. So did every other variant I could think of
>except changing to the directory where the file was and linking
>without any paths involved.

What was failing -- were you getting on OSError, or was the link
apparently being created without effect? And were you putting the names
in the right order? I can't find your original message, but the way ln
works is (if you're creating a symlink):

ln -s original_file filename_of_symlink

There's another gotcha, too, and this one is *not* obvious: the name by
which you reference original_file should be based on the directory the
symlink will go in. So given your example above, and assuming
burrows.html is the "real" file and Burrows.htm is the symlink you want
to create, then the command you really want to be running is:

ln -s burrows.html ./wombats/habitat/Burrows.htm

This will create the symlink Burrows.htm -> burrows.html in the
directory wombats/habitat. If you did:

ln -s ./wombats/habitat/burrows.html ./wombats/habitat/Burrows.htm

then what you would get would be: in the directory wombats/habitat,
there would be a symlink named Burrows.htm that tried to reference
wombats/habitat/wombats/habitat/burrows.html -- this is almost certainly
not what you wanted.

And as someone else suggested, you should probably be using os.symlink()
instead of os.system() do accomplish this:

$ mkdir foo
$ mkdir foo/bar
$ echo foo > foo/bar/orig
$ ls -l foo/bar
total 2
-rw-r--r--   1 rmunn    users          4 Feb 19 13:53 orig
$ python
Python 2.1.2 (#3, Feb  3 2002, 09:57:02)
[GCC 2.95.1 19990816 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> os.symlink
<built-in function symlink>
>>> print os.symlink.__doc__
symlink(src, dst) -> None
Create a symbolic link.
>>> os.symlink('orig','foo/bar/symlink')
>>> ^D
$ ls -l foo/bar
total 4
-rw-r--r--   1 rmunn    users          4 Feb 19 13:53 orig
lrwxrwxrwx   1 rmunn    users          4 Feb 19 13:54 symlink -> orig
$ cat foo/bar/orig
foo
$ cat foo/bar/symlink
foo
$ 

Hope this helps.

-- 
Robin Munn
rmunn at pobox.com



More information about the Python-list mailing list