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 248 views
1 answer 503 views
2 answers 347 views
1 answer 228 views
1 answer 179 views
2 answers 279 views
1 answer 565 views
...