How to read text file line by line in Python

5 Answers

0 votes
with open('d:\data.txt', mode='r') as file:
    for line in file:
        print(line)

'''
run:

Name: Ajax

Age: 27

Profession: Python Programmer

Name: Tim

Age: 37

Profession: Python , Java and PHP Programmer

'''

 



answered Sep 26, 2017 by avibootz
edited Dec 20, 2017 by avibootz
0 votes
fl = open("d:data.txt")
for line in fl:
    print(line)
fl.close()


'''
run:

python programming

'''

 



answered Dec 20, 2017 by avibootz
0 votes
filename = "d:\data.txt"

with open(filename) as f:
    content = f.readlines()

for line in content:
    print(line, end="")


'''
run:
   
c++ c c#
java php
python
 
'''

 



answered Jun 28, 2018 by avibootz
0 votes
filename = "d:\data.txt"

with open(filename) as f:
    content = f.read().splitlines()

for line in content:
    print(line)


'''
run:
   
c++ c c#
java php
python
 
'''

 



answered Jun 28, 2018 by avibootz
0 votes
import os.path

filename = "d:\data.txt"

if not os.path.isfile(filename):
    print('File not exist')
else:
    with open(filename) as f:
        content = f.read().splitlines()

    for line in content:
        print(line)


'''
run:
   
c++ c c#
java php
python
 
'''

 



answered Jun 28, 2018 by avibootz

Related questions

2 answers 246 views
3 answers 285 views
1 answer 175 views
2 answers 222 views
1 answer 201 views
2 answers 285 views
...