【问题标题】:Python seemingly not reading from text filePython似乎没有从文本文件中读取
【发布时间】:2017-07-06 14:18:35
【问题描述】:
files = []
with open("[...].log", "a+") as posshell:
    for line in files:
        print(line)
        posshell_files.append(line)

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

【问题讨论】:

标签: python printing lines


【解决方案1】:

您将不正确的第二个参数传递给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())

【讨论】:

  • 查看stackoverflow.com/a/1466036/3282436 以获得对文件模式的很好解释
  • 这行得通,但我的理解是我使用的应该是一个包罗万象的。编辑;感谢您的链接
  • @DylanMoore 请参阅上面的编辑以获取更多详细信息,但来自 0x5453 的链接是一个很好的总结
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多