[Tutor] list concatenation

Jeff Shannon jeff@ccvcorp.com
Wed Feb 5 12:45:01 2003


Lance wrote:

>Hi All,
>
>What's the fastest way to concatenate the same string to all elements of a
>list? I have a list of filenames and want to concatentate the path, a common
>path, to each of them.
>  
>

Since you're working with filenames, you'll want to use the os.path 
module.  To perform the same procedure on every element of a list, 
you're probably best off using a list comprehension.  Presuming that you 
have your common initial path in 'commonpath' and your list of files is 
called 'filenames':

import os

fullfilenames = [os.path.join(commonpath, name) for name in filenames]

Jeff Shannon
Technician/Programmer
Credit International