脚本在读取文件时跳过第二个循环 [英] Script skips second for loop when reading a file

查看:21
本文介绍了脚本在读取文件时跳过第二个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取日志文件并将某些值与预设阈值进行比较.我的代码设法使用函数中的第一个 for 循环记录原始数据.

I am trying to read a log file and compare certain values against preset thresholds. My code manages to log the raw data from with the first for loop in my function.

我添加了打印语句来尝试弄清楚发生了什么,并且我设法推断出我的第二个 for 循环永远不会发生".

I have added print statements to try and figure out what was going on and I've managed to deduce that my second for loop never "happens".

这是我的代码:

def smartTest(log, passed_file):    
    # Threshold values based on averages, subject to change if need be
    RRER = 5
    SER = 5
    OU = 5
    UDMA = 5
    MZER = 5
    datafile = passed_file
    # Log the raw data    
    log.write('=== LOGGING RAW DATA FROM SMART TEST===
')
    for line in datafile:
        log.write(line)
        log.write('=== END OF RAW DATA===
')

        print 'Checking SMART parameters...',
        log.write('=== VERIFYING SMART PARAMETERS ===
')

        for line in datafile:
            if 'Raw_Read_Error_Rate' in line:
                line = line.split()
                if int(line[9]) < RRER and datafile == 'diskOne.txt':
                    log.write("Raw_Read_Error_Rate SMART parameter is: %s. Value under threshold. DISK ONE OK!
" %int(line[9]))
                elif int(line[9]) < RRER and datafile == 'diskTwo.txt':
                    log.write("Raw_Read_Error_Rate SMART parameter is: %s. Value under threshold. DISK TWO OK!
" %int(line[9]))
                else:
                    print 'FAILED'
                    log.write("WARNING: Raw_Read_Error_Rate SMART parameter is: %s. Value over threshold!
" %int(line[9]))
                    rcode = mbox(u'Attention!', u'One or more hardrives may need replacement.', 0x30)

这就是我调用这个函数的方式:

This is how I am calling this function:

dataOne = diskOne()
smartTest(log, dataOne)
print 'Disk One Done'

diskOne() 看起来像这样:

def diskOne():
    if os.path.exists(r"C:DejeroHDD Guardian 0.6.1Smartctl"):
        os.chdir(r"C:DejeroHDD Guardian 0.6.1Smartctl")
        os.system("Smartctl -a /dev/csmi0,0 > C:DejeroInstallation-ScriptsdiskOne.txt")
        # Store file in variable
        os.chdir(r"C:DejeroInstallation-Scripts")
        datafile = open('diskOne.txt', 'rb')
        return datafile
    else:
        log.write('Smart utility not found.
')

我尝试在谷歌上搜索与我类似的问题,但没有找到.我尝试将我的第一个 for 循环移动到 diskOne() 中,但发生了同样的问题.没有语法错误,我现在无法看到问题.

I have tried googling similar issues to mine and have found none. I tried moving my first for loop into diskOne() but the same issue occurs. There is no syntax error and I am just not able to see the issue at this point.

推荐答案

它不会跳过您的第二个循环.您需要seek 返回位置.这是因为在读取文件后,文件偏移量会放在文件的末尾,因此您需要将其放回开头.这可以通过添加一行轻松完成

It is not skipping your second loop. You need to seek the position back. This is because after reading the file, the file offset will be placed at the end of the file, so you will need to put it back at the start. This can be done easily by adding a line

datafile.seek(0);

在第二个循环之前.

参考:文档

这篇关于脚本在读取文件时跳过第二个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆