Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,849 questions

9,751 answers

573 users

How to read an entire text file at once in Python

5 Answers

0 votes
s = open("d:\\data.txt").readlines()

print(s)


'''
run:

['python\n', 'java\n', 'c#\n', 'php\n']

'''

 




answered Dec 20, 2017 by avibootz
edited Dec 20, 2017 by avibootz
0 votes
s = open("d:\\data.txt").readlines()

print(s)
print("---------")
print(s[0])
print(s[1])


'''
run:

['python\n', 'java\n', 'c#\n', 'php\n']
---------
python

java

'''

 




answered Dec 20, 2017 by avibootz
edited Dec 20, 2017 by avibootz
0 votes
s = open("d:\\data.txt").read()

print(s)


'''
run:

python
java
c#
php

'''

 




answered Dec 20, 2017 by avibootz
edited Dec 20, 2017 by avibootz
0 votes
s = open("d:\\data.txt").read()

print(s)
print("-------")
print(s[0])
print(s[2])


'''
run:

python
java
c#
php

-------
p
t

'''

 




answered Dec 20, 2017 by avibootz
edited Dec 20, 2017 by avibootz
0 votes
s = open("d:\\data.txt").read()

print(s)
print("-------")
print(s[2:9])


'''
run:

python
java
c#
php

-------
thon
ja

'''

 




answered Dec 20, 2017 by avibootz
edited Dec 20, 2017 by avibootz
...