reading from list with paths

Larry Bates larry.bates at`
Wed Jun 25 17:55:09 CEST 2008

antar2 wrote:
> Hello,
> Suppose this is a stupid question, but as a python beginner I
> encounter a lot of obstacles... so I would be very grateful with some
> help for following question:
> I would like to read files, of which the complete filepaths are
> mentioned in another textfile. In this textfile (list.txt)  are for
> example the following paths:
> /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_1LG_f01.TextGrid
> /data/chorec/chorec-nieuw/s01/S01C001M1/
> S01C001M1_1LGPseudo_f01.TextGrid
> /data/chorec/chorec-nieuw/s01/S01C001M1/S01C001M1_AVI1_f01.TextGrid
> I know how to open and read one file in my current directory,
> but after trying to find this out my self, I give up...
> So could someone help me and write some code so that I know how to
> open and read the content of the mentioned files.
> Thanks a lot
> Antar2

You didn't say what you wanted to do with the data in each of the files, but 
perhaps this will get you going.

flist = open('list.txt', 'r')

for fpath in flist:
     fp = open(fpath, 'r')
     # Do something with data in this file
     for line in fp:



More information about the Python-list mailing list