Search code examples
pythonfor-loopreadlines

python beginner - how to read contents of several files into unique lists?


I'd like to read the contents from several files into unique lists that I can call later - ultimately, I want to convert these lists to sets and perform intersections and subtraction on them. This must be an incredibly naive question, but after poring over the iterators and loops sections of Lutz's "Learning Python," I can't seem to wrap my head around how to approach this. Here's what I've written:

#!/usr/bin/env python

import sys

OutFileName = 'test.txt'
OutFile = open(OutFileName, 'w')

FileList = sys.argv[1: ]
Len = len(FileList)
print Len

for i in range(Len):
    sys.stderr.write("Processing file %s\n" % (i))
    FileNum = i
    
for InFileName in FileList:
    InFile = open(InFileName, 'r')
    PathwayList = InFile.readlines()
    print PathwayList
    InFile.close()

With a couple of simple test files, I get output like this:

Processing file 0

Processing file 1

['alg1\n', 'alg2\n', 'alg3\n', 'alg4\n', 'alg5\n', 'alg6']

['csr1\n', 'csr2\n', 'csr3\n', 'csr4\n', 'csr5\n', 'csr6\n', 'csr7\n', 'alg2\n', 'alg6']

These lists are correct, but how do I assign each one to a unique variable so that I can call them later (for example, by including the index # from range in the variable name)?

Thanks so much for pointing a complete programming beginner in the right direction!


Solution

  • You could do it like that if you don't need to remeber where the contents come from :

    PathwayList = []
    for InFileName in FileList:
        sys.stderr.write("Processing file %s\n" % InFileName)
        InFile = open(InFileName, 'r')
        PathwayList.append(InFile.readlines())
        InFile.close()  
    
    for contents in PathwayList:
        # do something with contents which is a list of strings
        print contents  
    

    or, if you want to keep track of the files names, you could use a dictionary :

    PathwayList = {}
    for InFileName in FileList:
        sys.stderr.write("Processing file %s\n" % InFileName)
        InFile = open(InFileName, 'r')
        PathwayList[InFile] = InFile.readlines()
        InFile.close()
    
    for filename, contents in PathwayList.items():
        # do something with contents which is a list of strings
        print filename, contents