for loop specifying the amount of vars

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Nov 24 14:38:19 EST 2008


On Mon, Nov 24, 2008 at 2:31 PM, Jules Stevenson <lists at js3d.co.uk> wrote:

> Hi,
>
> I have a list which contains a folder structure, for instance:
>
> dirs=['c:\', 'temp', 'foo', 'bar']
>
> The length of the list can vary. I'd like to be able to construct a
> os.path.join on the list, but as the list can vary in length I'm unsure how
> to do this neatly. I figured I could use a for loop and build the whole
> statement as a string and 'eval it', but I'm aware that this is not a good
> idea.
>
> It strikes me that there probably is a very elegant way to achieve what I
> want to do, any pointers much appreciated.
>
> Cheers,
>
> Jules



>>> dirs = ['c:','temp','foo','bar']
>>> os.path.join(*dirs)
'c:\\temp\\foo\\bar'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081124/6fc68b9b/attachment-0001.html>


More information about the Python-list mailing list