Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

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 222 views
3 answers 259 views
1 answer 164 views
2 answers 204 views
1 answer 178 views
2 answers 211 views
...