Python在比较文件时不返回预期值

时间:2012-09-07 18:26:10

标签: python

我正在尝试构建此脚本以检查远程服务器上的文件是否包含set后缀IE .ok,.err,.log。我无法弄清楚为什么我得到了回报 "('\\\\THPNWSS5\\d$\\DA$Utils\\log\\networker', [], ['P3EWS.err']) nothing is equal
我知道我的try语句有问题,但我看不到它。

    chkbkpstats = csv.reader(file('c://temp//networkerservers1.csv'))

    srvrs = []
    for row in chkbkpstats:
        srvrs.append({'Name' : row[0], 'Instance' : row[1]})

    for srvr in srvrs:
        srvrName = (srvr['Name'])
        srvrInst = (srvr['Instance'])
        w2k3chk = r'\\%s\d$\DA$Utils\log\networker' % srvrName
        w2k8chk = r'\\%s\c$\ProgramData\folder\DA$Utils\log\networker' % srvrName

        try:
            c = wmi.WMI(srvr['Name'])
        except:
            print 'Error connecting to %s to check OS version' % srvrName

        else:
            osVer = c.Win32_OperatingSystem()[0].Caption
            if '2003' in osVer:
                for file in os.walk(w2k3chk):
                    print file
                    try:
                        if srvrInst == srvrInst + ".log":
                            print 'The Backup for %s on %s still running' % (srvrInst, srvrName)
                        if srvrInst == (srvrInst + ".ok"):
                            print 'Completed Successfully'
                        if srvrInst == (srvrInst + ".err"):
                            print 'Backup failed'
                    except:
                        print 'nothing is equal'
                #print '%s is w2k3' % srvr['Name']
            elif '2008' in osVer:
                print '%s is w2k8' % srvr['Name']

2 个答案:

答案 0 :(得分:2)

此行有问题

 if srvrInst == file.srvrInst + ".log":

应该是: -

if file == (srvrInst + ".log"):

文件没有srvrInst

修改后更新:

你怎么能认为这被评估为真: -

if srvrInst == (srvrInst + ".ok"):

假设srvrInst="John"

  

“John”==“John.ok”

永远可以成真????

答案 1 :(得分:0)

虽然@लंड指出了当前代码中最紧迫的错误,但他建议的修补程序file == (srcInst + ".log")也无效。那是因为os.walk函数不仅仅返回文件名。它返回一个三元组的生成器,每个元组由一个目录,一个子目录列表(也将被遍历)和一个文件列表组成。你需要一个额外的循环来遍历各个文件。

这样的事情应该有效:

found = False
for folder, subfolders, files in os.walk(w2k3chk):
    for file in files:
        if file == srvrInst + ".log":
            found = True
            print 'The Backup for %s on %s still running' % (srvrInst, srvrName)
        elif file == (srvrInst + ".ok"):
            found = True
            print 'Completed Successfully'
        elif file == (srvrInst + ".err"):
            found = True
            print 'Backup failed'
if not found:
    print 'No file found'

如果你确定在你正在搜索的文件夹中只有一个文件,你可以简化一下:

try:
    folder, subfolders, files = next(os.walk(w2k3chk)):
    file = files[0]
    if file == srvrInst + ".log":
        print 'The Backup for %s on %s still running' % (srvrInst, srvrName)
    elif file == (srvrInst + ".ok"):
        print 'Completed Successfully'
    elif file == (srvrInst + ".err"):
        print 'Backup failed'
    else:
        print 'Unknown file found: %s' % (file)
except (StopIteration, IndexError): # exceptions if folder or file doesn't exist
    print 'No file found'