Search code examples
pythontext

Python, cannot open a text file


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


Solution

  • 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