how to call os.path.join() on a list ...

funkyj funkyj at gmail.com
Mon Feb 26 23:13:01 EST 2007


On Feb 26, 8:03 pm, "funkyj" <fun... at gmail.com> wrote:
> I want to call os.path.join() on a list instead of a variable list of
> arguments.  I.e.
>
>     [scr-misc] (186:0)$ python
>     iPython 2.4 (#2, Feb 18 2005, 16:39:27)
>     [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
>     Type "help", "copyright", "credits" or "license" for more
> information.
>     m>>>
>     >>> import os
>     >>> import string
>     >>> p = os.environ['PWD']
>     >>> p
>     '/tmp/a/b/c/d'
>     >>> os.path.join(string.split(p, os.sep))
>     ['', 'tmp', 'a', 'b', 'c', 'd']
>     >>>
>
> the value returned by os.path.join() is obviously not the desired
> result ...
>
> Sure, I can hack my own version of os.path.join() by using os.sep but
> that does not seem very pythonic.
>
> In lisp one would do something like
>
>     (funcall  #'os.path.join  (string.split p os.sep))
>
> What is the python idiom for callling a function like os.path.join()
> that takes a variable number of arguments when you currently have the
> arguements in a list variable?
>
> I'm curious about the answer to the question above but in the meantime
> I'll hack "my.path.join()' that takes a single list as an argument and
> move on with my little project.
>
> Regards,
>   fj

figured it out ...

I can just do:

    os.sep.join(string.split(p, os.sep))

it isn't "funcall" but it gets me where I want to go.

Regards,
  --jfc




More information about the Python-list mailing list