file.write命令的奇怪行为

时间:2019-04-17 08:28:47

标签: python python-3.x

下面是较大代码中的一部分。我从ofile.write命令中看到了奇怪的行为。

def buildruleset(sl, irf, orf):
    for a, b, c in sl:
        with open(irf, 'r') as ifile, open(orf, 'a') as ofile:
            for k in ifile:
                eachk = k.split('\t')
                if a in eachk[7] or a in eachk[8]:
                    print(k)  # This is just for validation
                    ofile.write(k)

我在文件中看到的结果(由于ofile.write命令)如下:

  

10.190.40.48_29; MMS_10.190.40.45; SYS002_10.32.2.10; FKC_10.32.2.21 ;; DREAM_SKEW ;; snmp; snmp-trap; echo-r​​eply; icmp-requests; udp_traceroute日志DREAM_SKEW任何   新规则:SNMP规则; 12 security_rule SNMP规则偏斜SNMP规则     错误接受;; ss01_10.32.2.20; SS02_10.32.2.61; SYS002_10.32.2.10; FKC_10.32.2.21 ;; SKEW_DMZ_10.41.7.0_24; SKEW_10.41.6.240_29 ;; snmp; snmp-trap; echo-回复;回显请求日志DREAM_SKEW任意   新规则:

我在屏幕上看到的结果(作为打印命令的结果)如下:

  

11 security_rule SNMP RULES SKEW SNMP RULE false accept ;; ss01_10.32.2.20; SS02_10.32.2.61; MMS _ 10.190.40.48_29; MMS_10.190.40.45; SYS002_10.32.2.10 ; FKC_10.32.2.21 ;; DREAM_SKEW ;; snmp; snmp-trap; echo-r​​eply; icmp-requests; udp_traceroute日志DREAM_SKEW任意   新规则:SNMP规则; 12 security_rule SNMP规则偏斜SNMP规则     错误接受;; ss01_10.32.2.20; SS02_10.32.2.61; SYS002_10.32.2.10; FKC_10.32.2.21 ;; SKEW_DMZ_10.41.7.0_24; SKEW_10.41.6.240_29 ;; snmp; snmp-trap; echo-回复;回显请求日志DREAM_SKEW任意   新规则:

print结果正确,我希望在文件中看到相同的结果。为什么ofile.write仅部分打印(ofile.write命令遗漏了粗体文本)< / p>

我是python的新手,这是我第一个可用脚本的一部分。如何在Ofile中获得与print命令相同的输出?

编辑: 完整代码

# Objective of this program is to get input from user as one IP address, and
# - Find out which group objects they are part of (network objects are currently out of scope)
# - which rules are these host or group objects being called in

import os
import re


def buildsearhlist(sss, iof, sl):
    with open(iof, 'r') as infile:
        for i in infile:
            eachi = i.split("\t")
            if sss[0] in eachi[3]:
                sl.append([eachi[0], eachi[2], sss[0]])
                buildsearhlist([eachi[0], eachi[2]], iof, sl)


def buildruleset(sl, irf, orf):
    for a, b, c in sl:
        with open(irf, 'r') as ifile, open(orf, 'a') as ofile:
            for k in ifile:
                eachk = k.split('\t')
                if a in eachk[7] or a in eachk[8]:
                    print(k)
                    ofile.write(k)


def rulebasefiles(rbf):
    for i in os.listdir('rulebase'):
        if re.search('rulebase.tsv', i):
            rulebase_files.append(i)
    return rbf


# ipaddr = input('Which IP do you want to search? ')
# addrtype = input('Enter the type of object (host/network/group) Only 'host' supported at the moment: ')
ipaddr = '10.32.2.20'
addrtype = 'host'


starterset = [ipaddr, addrtype]
objfile = 'rulebase\\Objects.tsv'
searchlist = []
rulebase_files = []

buildsearhlist(starterset, objfile, searchlist)
rulebase_files = rulebasefiles(rulebase_files)

# print(searchlist)

for i in rulebase_files:
    targetfilename = ipaddr + "_" + re.search('^([\w\W]*)_rulebase.tsv', i).group(1) + "rules.tsv"
    with open('rulebase\\target\\'+targetfilename, 'w') as outfile:
        outfile.write("Rule no.\tAdminInfo\theader text\theader rule name\tname\tdisabled\taction\tsrc\tdst\tservices\ttrack\tinstall\tthrough\tcomments\n")
        buildruleset(searchlist, 'rulebase\\'+i, 'rulebase\\target\\'+targetfilename)

1 个答案:

答案 0 :(得分:0)

感谢提示@stovfl。 我减少了以下代码的意图,并解决了该问题

buildruleset(searchlist, 'rulebase\\'+i, 'rulebase\\target\\'+targetfilename)

也符合您的建议,在进入循环以避免此问题之前,我现在显式使用“ close”(请参见下面的示例)

            for m in [7, 8, 9]:
                for k in eachj[m].split(';'):
                    objecttree(k, objfile, targetfile_e)
                ofile = open(targetrulepath_expanded+i+'.csv', 'a')
                ofile.write("\",\"")
                ofile.close()  # coz module 'objecttree' has a 'with open' statement within
            ofile = open(targetrulepath_expanded+i+'.csv', 'a')
            ofile.write(eachj[11]+"\"\n")

。还有其他最佳做法吗?

相关问题