Argentina
Argentina

Reputation: 1141

Python: file object as function argument

I have written a function (read()) in a module I want to import in my main script: this function simply reads a file with Regular Expressions and creates an array from it.

The only parameter this function takes is the file (.txt, only numbers) it has to read. I would like to open the file in my main script data = open('output99.txt', 'r') giving the file the object data and then pass the file object data to the function, so that I can change the input file whenever I want..could that be a problem? Because if I do so, the function doesn't work, and returns an empty array:

def read(data):

n_lines = sum(1 for line in data)
array = np.empty((n_lines,13))
re_re = re.compile('^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)')

i=0
for line in data:
    reg = re_re.search(line)
    if(re!=None):
        array[i,0] = reg.group(1)
        array[i,1] = reg.group(2)
        array[i,2] = reg.group(3)
        array[i,3] =  reg.group(4)
        array[i,4] =  reg.group(5)  
        array[i,5] = reg.group(6)
        array[i,6] = reg.group(7)
        array[i,7] = reg.group(8)
        array[i,8] = reg.group(9)       
        array[i,9] = reg.group(10)
        array[i,10] = reg.group(11)
        array[i,11] = reg.group(12)
        array[i,12] = reg.group(13)

    i+=1



return array

If I open the file inside the function, it works correctly, but it would be far less quick when i want to change input file. Can anyone explain me that?

Upvotes: 0

Views: 1735

Answers (1)

chepner
chepner

Reputation: 530960

data is not a file name; it's a file object. When you read from it using

sum(1 for line in data)

you read the entire contents of the file, so that the file pointer is at the end of the file. When you try to read from it again with

for line in data:

you get no data, because you've already read everything in the file. To fix, you'll have to reset the file pointer after you count the number of lines with

data.seek(0)

Upvotes: 1

Related Questions