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

Related questions

1 answer 233 views
1 answer 487 views
2 answers 326 views
1 answer 220 views
1 answer 167 views
2 answers 262 views
1 answer 553 views
...