比较文件时Python未返回预期值

2024-05-16 10:58:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建此脚本,以检查远程服务器上的文件是否包含一组后缀,即.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']

Tags: nameinlogforifisutilsda
2条回答

虽然@2354;ंड指出了当前代码中最紧迫的错误,但他建议的修复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'

这条线有问题

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

应为:-

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

文件没有srvrInst。你知道吗

更新后编辑:-

你怎么能认为这是值得评价的呢正确:-你知道吗

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

假设srvrInst="John"

"John"=="John.ok"

会是真的吗????你知道吗

相关问题 更多 >