0

I am new to python and I have encounter this List is not callable error. What should I do to prevent this from happening? Thanks!

def PrintTxt(src, beg, end):
    with open(src, 'r') as f1:
        srctxt = f1.readlines()
        for line in srctxt(beg, end):
            print(line)

Filepath = 'C:\test.txt'

PrintTxt(Filepath,1,5)
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • 2
    If you're trying to slice the list, see https://stackoverflow.com/questions/509211/understanding-slice-notation – khelwood Sep 16 '19 at 15:13
  • 1
    You will need to escape the backslash: `Filepath = 'C:\test.txt'` -> `Filepath = 'C:\\test.txt'` or pass a raw string: `Filepath = r'C:\test.txt'` – EdChum Sep 16 '19 at 15:14
  • You could also use `islice(f1, beg, end)` from itertools. – mkrieger1 Sep 16 '19 at 15:16

1 Answers1

0

You seem to be unaware of the correct Python slice notation. After:

srctxt = f1.readlines()  # now srctxt is a list

you can slice the resulting list like:

for line in srctxt[beg:end]:
    # ....
user2390182
  • 72,016
  • 6
  • 67
  • 89