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

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

问题描述

我试图读取一个日志文件,并将某些值与预设的阈值进行比较。我的代码设法记录我的函数中的循环的第一个的原始数据。



语句,试图找出发生了什么,我已经设法推断,我的第二个循环从未发生。

这是我的代码:

  def smartTest(log,passed_file):
#基于平均值的阈值,如有需要可以更改
RRER = 5
SER = 5
OU = 5
UDMA = 5
MZER = 5
datafile = passed_file
#记录原始数据
log.write('===从SMART TEST记录原始数据=== \r\\\
')
用于数据文件中的行:
log.write(line)
log .write('=== END DATA OF RAW DATA === \r\\\
')

print'正在检查SMART参数...',
log.write('=' ==验证智能参数=== \r\\\
')

用于数据文件中的行:
如果'Raw_Read_Er ror_Rate':
line = line.split()
if(int [line] [9])< RRER和datafile =='diskOne.txt':
log.write(Raw_Read_Error_Rate SMART参数是:%s。阈值以下的值。DISK ONE OK!\r\\\
%int(line [9] ))
elif int(line [9])< RRER和datafile =='diskTwo.txt':
log.write(Raw_Read_Error_Rate SMART参数是:%s。阈值以下的值。DISK TWO OK!\r\\\
%int(line [9] ))
else:
print'FAILED'
log.write(WARNING:Raw_Read_Error_Rate SMART parameter is:%s。Value over threshold!\r\\\
%int(line
rcode = mbox(u'Attention!',u'One or more hardrives may need replacement。',0x30)

这就是我调用这个函数的方法:
$ b $ $ $ $ $ $ $ $ dataOne = diskOne()
smartTest(log,dataOne)
print'Disk One Done'

diskOne()看起来像这样:

  def diskOne():
if os.path.exists(rC:\Dejero\HDD Guardian 0.6.1\Smartctl):
os.chdir(rC:\Dejero\HDD Guardian 0.6。 1 \Smartctl)
os.system(Sm将文件保存在变量
中os.chdir(rC:\\ artetl -a / dev / csmi0,0> C:\Dejero\Installation-Scripts\diskOne.txt)
# \\'Dejero \Installation-Scripts)
datafile = open('diskOne.txt','rb')
返回数据文件
else:
log.write('Smart utility没有找到。\r\\\
')

我试过Google搜索类似的问题没有发现。我试着将我的第一个循环放入 diskOne(),但同样的问题发生。没有语法错误,我现在只是无法看到这个问题。

解决方案

它不会跳过你的第二个循环。你需要寻找位置回来。这是因为在读取文件之后,文件偏移量将被放置在文件末尾,因此您需要在开始时重新放置文件。这可以很容易地通过添加一行

  datafile.seek(0); 

在第二次循环之前。

文档


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.

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".

This is my code:

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===\r\n')
    for line in datafile:
        log.write(line)
        log.write('=== END OF RAW DATA===\r\n')

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

        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!\r\n" %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!\r\n" %int(line[9]))
                else:
                    print 'FAILED'
                    log.write("WARNING: Raw_Read_Error_Rate SMART parameter is: %s. Value over threshold!\r\n" %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() looks like this:

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

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.

解决方案

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);

Before the second loop.

Ref: Documentation

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

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