file.read返回空列表

时间:2018-12-06 09:42:20

标签: python python-3.x file file.readalllines

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

我已经以“ r +”打开文件 该文件包含内容,但是代码返回以下内容:

[]
None

我确实从头开始,但仍然没有返回内容 任何帮助表示赞赏!

这是完整的代码:

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)

写入功能可以正常工作

1 个答案:

答案 0 :(得分:0)

您已经使用读取了文件

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

这会将文件的内容返回到readd变量。下一行,

print(file1.readlines())

将再次调用readlines,这将返回一个空列表。