Dynamic File Name Open()

Tim Golden mail at timgolden.me.uk
Wed May 2 03:47:30 EDT 2007


Robert Rawlins - Think Blue wrote:
> I'm trying to open a file using open() but the name of the file is created
> dynamically as a variable, but also has part of a static path. For instance,
> the file may be called 'dave' and will always be in '/my/files/here/'.

Well that's an absolutely normal way of doing
things, so if my toy example below doesn't make
things clear, you'd better post a code fragment
and/or some traceback.

<code>
import os, sys

path = "c:/temp"
for filename in ["chas.txt", "dave.txt"]:
   f = open (os.path.join (path, filename))
   print filename
   print f.read ()
   print
   f.close ()

</code>

TJG



More information about the Python-list mailing list