我正在尝试从多个文本(nmap)文档中收集特定行,然后以表格格式创建一个新文件。我还没有进入桌面部分,因为我无法获得附加工作。
#imports
import os
#Change directories
os.chdir ("M:\\Daily Testing")
#User Input
print "What is the name of the system being scanned?"
sys = raw_input("> ")
#Subfolder selected
os.chdir (sys)
os.chdir ("RESULTS")
#variables
tag = ["/tcp", "/udp"]
fout = [sys + " Weekly Summary.csv"]
import glob
for filename in glob.glob("*.nmap"):
with open(filename, "rU") as f:
for line in f:
if not line.strip():
continue
for t in tag:
if t in line:
fout.write(line)
else:
continue
答案 0 :(得分:3)
您忽略了打开要追加的文件(fout
是一个列表,而不是文件对象,因此它没有.write()
方法)。
更改行
fout = [sys + " Weekly Summary.csv"]
到
with open(sys+" Weekly Summary.csv", "w") as fout:
并相应地缩进以下行。
所以,像这样:
<snip>
import glob
with open(sys + " Weekly Summary.csv", "w") as fout:
for filename in glob.glob("*.nmap"):
with open(filename, "rU") as f:
for line in f:
if not line.strip():
continue
for t in tag:
if t in line:
fout.write(line)
else:
continue