By Alexander West


2018-03-13 15:03:08 8 Comments

I´m having some trouble with reading a file line-by-line, instead of reading the the file into memory. As of right now, I'm reading the file into memory, and it works perfect. However, If i try to read the file line-by-line, I only get zero when I type 'print(B)'. My question is, does anyone have a good command for reading the file line-by-line in python? My code looks like this:

def read(filename):

    with open(filename, 'r') as f: #open the file

        for line in f:

            A = sum(float(line) for line in f)

    with open(filename, 'r') as f:

            B = sum(float(line)**2 for line in f)

            print(B)

read('file.txt')

4 comments

@Paul Brown 2018-03-13 15:52:52

Original problem is that you've hit the end of the file and need to go back to the start to iterate over it again. You can do this in a single iteration through the file, split into two and then sum.

with open(filename) as f:
    A, B = map(sum, zip(*((x, x**2) for x in map(float, f))))

@Rahul 2018-03-13 15:20:21

Is this right for you?

with open(filename, 'r') as f:
    data = f.readlines()

A = sum(float(line) for line in data)
B = sum(float(line)**2 for line in data)

@Alexander West 2018-03-13 15:34:07

Thanks! This was exactly what I was looking for.

@Rahul 2018-03-13 15:55:58

You can accept useful answers

@kutschkem 2018-03-13 15:12:15

To return to the beginning of the file, use seek:

def read(filename):

    with open(filename, 'r') as f: #open the file

        A = sum(float(line) for line in f)
        f.seek(0)
        B = sum(float(line)**2 for line in f)

            print(B)

@Alexander West 2018-03-13 15:34:51

Thanks for your help! Your input was useful.

@Graipher 2018-03-13 15:09:55

Here is a way to do it with only one pass over the file. You have to abandon the nice built-in sum and do it yourself:

def read(filename):
    A, B = 0, 0
    with open(filename) as f:
        for line in f:
            x = float(line)
            A += x
            B += x**2
    print(A)
    print(B)

Also note that you are actually iterating in a weird way over the lines of the file, since you have an outer loop for line in f and an inner loop in the sum that also runs over for line in f. Since f is an iterator, this means that the outer loop will only get to the first line, the inner loop will consume all other lines and sum them and then the outer loop has nothing else to process and quits. You should be able to see this by noting that the print(B) statement is only executed once.

Related Questions

Sponsored Content

40 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

22 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 2797041 View
  • 3123 Score
  • 22 Answer
  • Tags:   python directory

31 Answered Questions

[SOLVED] How to read a file line-by-line into a list?

14 Answered Questions

[SOLVED] How do I install a Python package with a .whl file?

52 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

14 Answered Questions

[SOLVED] How do I copy a file in Python?

10 Answered Questions

7 Answered Questions

[SOLVED] How do you append to a file?

  • 2011-01-16 16:20:33
  • user502039
  • 1323371 View
  • 1263 Score
  • 7 Answer
  • Tags:   python file append

7 Answered Questions

[SOLVED] Catch multiple exceptions in one line (except block)

19 Answered Questions

[SOLVED] Python progression path - From apprentice to guru

  • 2010-04-04 00:28:33
  • Morlock
  • 339281 View
  • 659 Score
  • 19 Answer
  • Tags:   python

Sponsored Content