Advertisement
ksieradzinski

Untitled

Mar 19th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. class FileIterator:
  2. def __init__(self, filename: str):
  3. self.filename = filename
  4. self.file = open(filename, "r", encoding="utf8")
  5.  
  6. def __iter__(self):
  7. return self
  8.  
  9. def __next__(self):
  10. line = self.file.readline()
  11. if not line:
  12. self.file.close()
  13. raise StopIteration
  14.  
  15. return line.strip()
  16.  
  17. iter = FileIterator("osoby.txt")
  18. print(next(iter))
  19. print("---" * 10)
  20.  
  21. for line in iter:
  22. print(line)
  23.  
  24. # print(next(iter))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement