Python Forum
Do not get how Python iterates over a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do not get how Python iterates over a file
#13
You could use fileinput to read many files.

import fileinput


files = fileinput.input(("file1.txt", "file2.txt"), encoding="utf8")

for line in files:
    lineno = files.lineno()
    filename = files.filename()
    filelineno = files.filelineno()
    print(f"[{filename:<15}] [{filelineno:>4d}]: {line}", end="")
So, if you need the current file, line number and total line number, you could use this.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Do not get how Python iterates over a file - by DeaD_EyE - Jan-29-2023, 01:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Still do not get how Python iterates over a file tester_V 13 3,745 Aug-24-2021, 05:10 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020