Re: for loop specifying the amount of vars

2008-11-24 Thread Steve Holden
Jules Stevenson wrote:
> Hi,
> 
> I have a list which contains a folder structure, for instance:
> 
> dirs=['c:\', 'temp', 'foo', 'bar']
> 
Of course this should really be

  dirs=['c:\\', 'temp', 'foo', 'bar']

but we'll overlook your little syntax error ;-)

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

Don't reply to someone else's post with a new question, please: many
people use "threaded" readers, and will not even see your subject line.

What you need is

  os.path.join(*dirs)

which tells Python to take the list and turn it into separate arguments.
Fortunately os.path.join will take as many arguments as you care to pass it:

>>> os.path.join(*dirs)
'c:\\temp\\foo\\bar'
>>>

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

--
http://mail.python.org/mailman/listinfo/python-list


RE: for loop specifying the amount of vars

2008-11-24 Thread Jules Stevenson
Sorry for the noise, I found the * unpack operator. Perfect for what I need.

> 
> 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
> 
> --
> http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list


Re: for loop specifying the amount of vars

2008-11-24 Thread Tim Chase

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. 


Sounds like you want argument unpacking:

  >>> dirs=['c:\\', 'temp', 'foo', 'bar']
  >>> print os.path.join(*dirs)
  c:\temp\foo\bar

(side note:  you can't have a single trailing backslash like your 
example assignment)


The asterisk instructs python to unpack the passed list as if 
each one was a positional argument.  You may occasionally see 
function definitions of the same form:


  def foo(*args):
for arg in args:
  print arg
  foo('hello')
  foo('hello', 'world')
  lst = ['hello', 'world']
  foo(*lst)

You can use "**" for dictionary/keyword arguments as well.  Much 
more to be read at [1].


-tkc


[1]
http://www.network-theory.co.uk/docs/pytut/KeywordArguments.html


--
http://mail.python.org/mailman/listinfo/python-list


Re: for loop specifying the amount of vars

2008-11-24 Thread Benjamin Kaplan
On Mon, Nov 24, 2008 at 2:31 PM, Jules Stevenson <[EMAIL PROTECTED]> 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'
--
http://mail.python.org/mailman/listinfo/python-list