Skip to content Skip to sidebar Skip to footer

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 on

Solution 1:

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)

Post a Comment for "Python: File Object As Function Argument"