Open a List of Files

Fredrik Lundh fredrik at pythonware.com
Tue Jan 8 07:16:42 EST 2008


BJ Swope wrote:

>     the code looks ok.  please define "not working".
> 
> Yep, defining "not working" is always helpful! :)
> 
> I want to have all 3 files open at the same time.  I will write to each 
> of the files later in my script but just the last file is open for writing.

to keep more than one file open, you need more than one variable, or a 
variable that can hold more than one object.

if the number of files may vary, put the file objects in a list.  if the 
number of files is constant, you might as well just use three variables 
and call open three times; e.g.

   def getfilename(name):
     return os.path.join(Host_Path, name)

   messages_file = open(getfilename("messages"), "w")
   recipients_file = open(getfilename("recipients"), "w")
   viruses_file = open(getfilename("viruses"), "w")

</F>




More information about the Python-list mailing list