473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 9769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pierre Fortin | last post by:
This quest for understanding started very innocently... A simple error on my part, passing on args as "args" instead of "*args" to os.path.join() led me to wonder why an error wasn't raised... ...
46
by: Leo Breebaart | last post by:
I've tried Googling for this, but practically all discussions on str.join() focus on the yuck-ugly-shouldn't-it-be-a-list-method? issue, which is not my problem/question at all. What I can't...
6
by: lampshade | last post by:
Hello, I'm having some problems with os.path.isdir I think it is something simple that I'm overlooking. #!/usr/bin/python import os my_path = os.path.expanduser("~/pictures/") print...
8
by: kj | last post by:
How can a script know its absolute path? (__file__ only gives the path it was used to invoke the script.) Basically, I'm looking for the Python equivalent of Perl's FindBin. The point of all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.