python – 脚本在读取文件时跳过第二个for循环
发布时间:2020-12-20 11:41:19 所属栏目:Python 来源:网络整理
导读:我正在尝试读取日志文件并将某些值与预设阈值进行比较.我的代码设法使用我的函数中的第一个for循环来记录原始数据. 我已经添加了print语句来试图弄清楚发生了什么,并且我设法推断出我的第二个for循环永远不会“发生”. 这是我的代码: def smartTest(log,pas
我正在尝试读取日志文件并将某些值与预设阈值进行比较.我的代码设法使用我的函数中的第一个for循环来记录原始数据.
我已经添加了print语句来试图弄清楚发生了什么,并且我设法推断出我的第二个for循环永远不会“发生”. 这是我的代码: 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===rn') for line in datafile: log.write(line) log.write('=== END OF RAW DATA===rn') print 'Checking SMART parameters...',log.write('=== VERIFYING SMART PARAMETERS ===rn') 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!rn" %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!rn" %int(line[9])) else: print 'FAILED' log.write("WARNING: Raw_Read_Error_Rate SMART parameter is: %s. Value over threshold!rn" %int(line[9])) rcode = mbox(u'Attention!',u'One or more hardrives may need replacement.',0x30) 这就是我调用这个函数的方式: 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.rn') 我曾尝试用谷歌搜索类似的问题,但没有找到.我尝试将我的第一个for循环移动到diskOne()但是会出现同样的问题.没有语法错误,我现在无法看到问题. 解决方法
它不会跳过你的第二个循环.你需要找回位置.这是因为在读取文件后,文件偏移量将放在文件的末尾,因此您需要在开始时将其放回原处.这可以通过添加一行来轻松完成
datafile.seek(0); 在第二个循环之前. 参考:Documentation (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |