469,631 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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


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

Feb 27 '07 #1
3 8897
On Feb 26, 8:03 pm, "funkyj" <fun...@gmail.comwrote:
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

Feb 27 '07 #2
"funkyj" <fu****@gmail.comwrites:
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" <fu****@gmail.comwrites:
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

Feb 27 '07 #3
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
Feb 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.