简体   繁体   English

file.read返回空列表

[英]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: 我已经以“ r +”打开文件,文件包含内容,但是代码返回以下内容:

[]
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 写功能正常工作顺便说一句

you already read the file using 您已经使用读取了文件

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

this will returns the content of file to the readd variable. 这会将文件的内容返回到readd变量。 The next line, 下一行

print(file1.readlines())

will again call readlines which will return an empty list. 将再次调用readlines,这将返回一个空列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM