confusion regarding os.path.walk()

Steven Majewski sdm7g at Virginia.EDU
Tue Feb 19 13:02:08 EST 2002


On Tue, 19 Feb 2002, Andrew Brown wrote:

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

You *can* create symlinks in a directory that's a relative subdirectory.

You and Ron seem to be having the same sort of confusion.

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.

The current working directory stays the same and all relative
paths are relative to it. When os.path.walk gives you a directory
and a list of files in that directory, unless that directory is
your current working directory, you need to either join the paths
or change your working directory to access the files.


-- Steve Majewski







More information about the Python-list mailing list