A
A
Alexey Denisov2020-08-03 14:02:30
Python
Alexey Denisov, 2020-08-03 14:02:30

How to list all lines from a file?

I can't figure out how to list all lines from a file?
I found such a method, but it reads the file line by line, and I need to open the file, read its contents into a variable (about 200 lines) and close it, then work with this variable.
Since the process there will be long, and you will not have to keep the file open.

for x in open("DATA.txt", "r").readlines():    
    print(x)

Need to process each row in a for loop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-08-03
@Denisov80

with open("DATA.txt") as file:
  lines = file.read().splitlines()

for line in lines:
  print(line)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question