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

Discussion in 'Python' started by funkyj, Feb 27, 2007.

  1. funkyj

    funkyj Guest

    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
     
    funkyj, Feb 27, 2007
    #1
    1. Advertising

  2. funkyj

    funkyj Guest

    On Feb 26, 8:03 pm, "funkyj" <> 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
     
    funkyj, Feb 27, 2007
    #2
    1. Advertising

  3. funkyj

    Ben Finney Guest

    "funkyj" <> writes:

    > I want to call os.path.join() on a list instead of a variable list of
    > arguments. I.e.
    >
    > [...]
    > >>> 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 ...


    Nor is the value returned by the string 'split' function quite what
    you describe.

    >>> p.split(os.sep)

    ['', 'tmp', 'a', 'b', 'c', 'd']
    >>> os.path.split(p)

    ('/tmp/a/b/c', 'd')

    > [...]
    > 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?


    >>> import os
    >>> p = ["/tmp", "a", "b", "c", "d"]
    >>> os.path.join(*p)

    '/tmp/a/b/c/d'

    "funkyj" <> writes:

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


    It also isn't 'os.path.join'.

    >>> p = ["/tmp", "a", "b/", "c/", "d"]
    >>> os.sep.join(p)

    '/tmp/a/b//c//d'
    >>> os.path.join(*p)

    '/tmp/a/b/c/d'

    --
    \ "Never use a long word when there's a commensurate diminutive |
    `\ available." -- Stan Kelly-Bootle |
    _o__) |
    Ben Finney
     
    Ben Finney, Feb 27, 2007
    #3
  4. funkyj

    greg Guest

    funkyj wrote:

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


    os.path.join(*list_of_args)

    This is preferable to joining it yourself with
    os.path.sep, because it will do the right thing
    for the platform, which might not be so simple
    in all cases.

    --
    Greg
     
    greg, Feb 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    905
    Alan Silver
    Jun 5, 2006
  2. Pierre Fortin

    args v. *args passed to: os.path.join()

    Pierre Fortin, Sep 18, 2004, in forum: Python
    Replies:
    2
    Views:
    721
    Pierre Fortin
    Sep 18, 2004
  3. googleboy
    Replies:
    1
    Views:
    947
    Benji York
    Oct 1, 2005
  4. Steve
    Replies:
    1
    Views:
    994
    Fredrik Lundh
    Dec 13, 2005
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    204
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page