2017-07-06 68 views
-2
files = [] 
with open("[...].log", "a+") as posshell: 
    for line in files: 
     print(line) 
     posshell_files.append(line) 

我沒有線索。它什麼都不打印。該數組是空的。我試圖抓住每個空字符,並刪除它們,如果它是UTF16 - >以UTF8打開,則無法使用。Python似乎沒有從文本文件中讀取

+1

如果你想讀寫使用'r +'。 https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files – Anthon

+0

請從您的問題中刪除信息,人們花時間回答它。 –

回答

2

你逝去的不正確的第二個參數open調用讀取這樣的文件:

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

根據Python文檔爲open'r'如果讀書時,'a+'默認的標誌是閱讀和寫入,但你必須以不同的方式這樣做:

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

看到https://stackoverflow.com/a/1466036/3282436文件模式的一個很好的解釋 – 0x5453

+0

這工作,但我的理解是,我正在使用一個應該是一個捕獲所有。編輯;感謝您的鏈接 –

+0

@DylanMoore查看上面的更多細節的編輯,但從0x5453鏈接是一個不錯的總結 –

0

試試這個

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