[英]Python: How do I use this code snippet to append text to the next line?
我制作了一個基於 python 的問答游戲。 我的一個函數從文件中讀取文本並將其作為字典導入以用作問題/答案庫。 我有一個名為 nmapQuestions.txt 和 portQuestions.txt 的文件。 我打算不斷添加問題頁面。 我正在使用下面的代碼片段使我的目錄中的一個文本文件在一個文檔中包含所有問題。 如果你願意,可以做一個大師測驗。 我有一個問題。 當這將 2 個文件附加在一起時,第二個文件不會從新行開始,而是從最后一個文件的行尾開始,如何將其附加到新行?
def master_list():
outfilename = "zMasterList.txt"
with open(outfilename, 'wb') as outfile:
for filename in glob.glob('*.txt'):
if filename == outfilename:
continue
with open(filename, 'rb') as readfile:
shutil.copyfileobj(readfile, outfile)
輸出:
什么是端口 -T4?:,強度
什么是端口 -sS?: ,syn scan
什么是端口 -sA?: ,ack scan
什么是 port -p?: ,port scan
什么是端口 -sT?: ,tcp scan
什么是端口 -sW?: ,windows scan
什么是端口 -sM?: ,maimon scan
什么是端口 -iL?: ,scan from file
什么是端口 -iR?: ,隨機掃描
什么是端口 -sU?: ,udp scan什么是端口 15?: ,netstat <--- 2 在同一行
什么是端口 20?:,ftp 數據
什么是端口 21?:,ftp 命令
什么是端口 22?:,ssh
什么是 23 端口?:,telnet 遠程登錄
什么是 25 端口?:,smtp
什么是 53 端口?:,dns
什么是80端口?:,http
什么是端口 110?:,郵局協議
什么是端口 111?: ,rcp bind
請絕對感謝您的幫助,謝謝
似乎您應該確保您的文件以換行符結尾。
一種解決方案是先附加換行符,然后復制文件的其余部分。
所以你只需要添加:
print(file=outfile)
這將在您的outfile
添加一個新行。
如果要添加 2 行,請改為:
print("\n", file=outfile)
在做了一些研究之后,我發現您可以使用以下代碼在塊的末尾添加一個新行:
outfile.write(b"\\n")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.