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

F

funkyj

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
 
F

funkyj

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
 
B

Ben Finney

funkyj said:
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 said:
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'
 
G

greg

funkyj said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top