Python 似乎没有从文本文件中读取

Python seemingly not reading from text file

files = []
with open("[...].log", "a+") as posshell:
    for line in files:
        print(line)
        posshell_files.append(line)

我不知道。它什么都不打印。该数组为空。我试过抓取每个空字符并删除它们,以防它是 UTF16 -> 打开为 UTF8,但没有用。

您将不正确的第二个参数传递给 open 调用以这种方式读取文件:

posshell_files = []
with open("posshell.log", "r") as posshell:
    for line in posshell:
        print(line)
        posshell_files.append(line)

根据 open 的 Python 文档,'r' 如果读取的默认标志而 'a+' 用于读取和写入,但您必须这样做以不同的方式:

with open("posshell.log","a+") as f:
    f.seek(0)
    print(f.read())

试试这个

with open('posshell.log') as p:
    content = p.readlines()
    content = [x.strip() for x in content]