Javier Albarracin
Javier Albarracin

Reputation: 69

file.read returning empty list

file1.seek(0)
exec(i + ".seek(0)")
readd = exec(i + ".readlines()")
print(file1.readlines())

I have already opened the file as "r+" the file contains content, but the code returns this:

[]
None

I did seek to the beginning but it still doesn't return the contents any help is appreciated!

this is the full code:

dictt = {'a':'z','b':'x','c':'c','d':'v','e':'b',
    'f':'n','g':'m','h':'a',
    'i':'s','j':'d','k':'f','l':'g','m':'h',
    'n':'j','o':'k','p':'l','q':'ñ',
    'r':'q','s':'w','t':'e','u':'r','v':'t',
    'w':'y','x':'u','y':'i','z':'o', "ñ" : "p",
    " " : "m", "\\" : "m"
    }


inv_map = {v: k for k, v in dictt.items()}

def read():
    file1 = open("memory1.txt", "r+")
    i = input("file: ")
    o = input("Note: ")
    exec(i + ".seek(0)")
    readd = exec(i + ".readlines()")
    readd = file1.readlines()
    readd = str(readd)
    lent = len(readd)
    final = ""
    for i in range(lent - 4):
        final += inv_map[str(readd[i + 2])]
    print(final)
    print("Note: " + o)
    file1.close()

def write():
    file1 = open("memory1.txt", "r+")
    n = input("to file: ")
    m = str(input("Go: "))
    print(n)
    lent = len(m)
    for i in range(lent):
    exec(n + ".write(dictt[m[i]])")
    print(dictt[m[i]])
    print("Wrote: ", m)
    file1.close()

while (cont == True):
    y = input()
    print(".")
    final = y + "(" + ")"
    exec(final)

the write function works fine btw

Upvotes: 1

Views: 808

Answers (1)

Amit Nanaware
Amit Nanaware

Reputation: 3356

you already read the file using

readd = exec(i + ".readlines()")

this will returns the content of file to the readd variable. The next line,

print(file1.readlines())

will again call readlines which will return an empty list.

Upvotes: 0

Related Questions