-2

我正在尝试从多个文本(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
4

1 回答 1

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
于 2012-10-01T15:47:59.753 回答