Dynamic File Name Open()

tahiriman tahiri.ahmed at gmail.com
Wed May 2 08:19:07 EDT 2007


On May 2, 7:47 am, Tim Golden <m... at timgolden.me.uk> wrote:
> 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


you have to look at the method open(string path, string mode)
properties
so, you can change the argument, string path,  dynamicly




More information about the Python-list mailing list