2

I have a text file named android.txt (with a couple of thousand of lines) which I try to open with python, my code is:

f = open('/home/user/android.txt', 'r')

But when I'm doing:

f.read()

the result is:

''

I chmod 777 /home/user/android.txt but the result remains the same

Drakes
  • 23,254
  • 3
  • 51
  • 94
woshitom
  • 4,811
  • 8
  • 38
  • 62
  • Try using `f.readlines()` – Bharadwaj Jun 29 '15 at 07:23
  • 3
    Duplicate of [this](http://stackoverflow.com/questions/10571399/python-not-reading-file-returning-empty-result) – Vaulstein Jun 29 '15 at 07:24
  • show the [*complete* (but minimal) code example](http://stackoverflow.com/help/mcve). Are you reading from the file more than once before printing it? (e.g., `f.read(); print(repr(f.read()))` -- don't do that) – jfs Jun 29 '15 at 08:29

2 Answers2

3

You are not displaying the contents of the file, just reading it.

For instance you could do something like this:

with open('/home/user/android.txt') as infp:
    data = infp.read()

print data # display data read 

Using with will also close the file for your automatically

Vaulstein
  • 20,055
  • 8
  • 52
  • 73
1

The result would be empty string not empty list and it's because your file size is larger than your memory(based on your python version and your machine)! So python doesn't assigned the file content to a variable!

For getting rid of this problem you need to process your file line by line.

with open('/home/user/android.txt') as f :
    for line in f:
         #do stuff with line
Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 2
    there is not such thing as *"python string object capacity"*. You might get `MemoryError` but you won't get an empty string just because a file is too large to fit in memory. – jfs Jun 29 '15 at 08:35
  • @J.F.Sebastian Indeed.I just wanted to make it understandable for OP. Actually it's the memory and can be used for any object! – Mazdak Jun 29 '15 at 08:46