[Tutor] os.symlink can't find target

Cameron Simpson cs at zip.com.au
Wed Feb 26 06:31:14 CET 2014


Hi Bob,

I notice your problem is solved, but I've got a few remarks about
your script and also how you might have investigated your problem.

First: the os.foo calls are usually very thin wrappers for the
corresponding OS call. So if you get an errno type error for
os.symlink, it is worth running:

  man 2 symlink

to read the man page for the symlink OS call. Amongst other things
it says, under ERRORS:

  ENOENT A directory component in newpath does not exist or is a dangling
         symbolic link, or oldpath is the empty string.

You need to know that ENOENT is errno 2 "No such file or directory",
but it helps.

The other things are just script remarks, not directly related to your problem:

On 24Feb2014 16:07, Bob Williams <linux at barrowhillfarm.org.uk> wrote:
> if pathList[j][-3:] == "mp3":

This is often written (I've inserted a dot, assuming you don't want
"foomp3", only "foo.mp3"):

  if pathList[j].endswith(".mp3"):

More readable, and also more reliable because it doesn't rely on
you getting the "3" correct.

>     linkName1 = pathList[j][0:-3] + "mp3"

Isn't this exactly the same as pathList[j] ?

>     linkName2 = destPath + linkName1[len(sourcePath):]

You might want to spell this:

      linkName2 = os.path.join(destPath, linkName1[len(sourcePath):])

This means you don't need to have trailing slashes on sourcePath and
destPath. And, to a degree, it might work on non-UNIX systems where
the path separator is not a slash. Ireelevant for you now, but it
is often a good habit to use portable ways of working on things if
it is not inconvenient.

[...snip...]
> I have tried escaping the spaces with '\', but the same error is
> generated.

This is usually never the right thing to do. If you find yourself
doing this, chances are you're trying to fix the wrong thing. You
only need to escape strings when passing them to something that
will be interpreting a line of text. Such as a piece of shell script.
You should not need it to pass strings to something that expects
strings.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil. - Donald Knuth


More information about the Tutor mailing list