python - If "string" in variable: not working Python3 -
the problem: string specified not being found in text file, why?
description: i've got simple python script here checks see if file exists, if does, check integrity, if passes, stop. if fails, recreate file. if file doesn't exist make it.
i've got working integrity check. integrity check right looking a string called "[drivec]", i'd make more thorough i've got going far.
any thoughts? work around convert config file list variable , search through list string. i'd use method seems scalable.
my code: (also can seen here https://hastebin.com/umitibigib.py) line 55 check failing
###io testing import os.path try: configparser import configparser except importerror: configparser import configparser # ver. < 3.0 #variables drives_given = [ 'c', 'd'] # instantiate config parser config = configparser() cfg_path = os.path.exists('smartpyconfig.ini') #a config file not found, let's make 1 def create_config_file(): cfgfile = open("smartpyconfig.ini",'w') print("a new config file created") print("") print("adding thresholds , drive sections") #add general settings config.add_section('general') config.set('general', 'logging_level', 'debug') #add smartctl threshold values config.add_section('standard_thresholds') config.set('standard_thresholds', 'threshold_value_raw_read_error_rate_norm', '101') config.set('standard_thresholds', 'threshold_value_reallocated_sector_count_norm', '105') config.set('standard_thresholds', 'threshold_value_seek_error_rate_norm', '101') config.set('standard_thresholds', 'threshold_value_power_on_hours_raw', '1000') config.set('standard_thresholds', 'threshold_value_temperature_celsius_raw', '100') config.set('standard_thresholds', 'threshold_value_reported_uncorrect_raw', '100') config.set('standard_thresholds', 'threshold_value_hardware_ecc_recovered_norm', '100') config.set('standard_thresholds', 'threshold_value_offline_uncorrectable_raw', '100') config.set('standard_thresholds', 'threshold_value_free_fall_sensor_raw', '100') config.set('standard_thresholds', 'threshold_value_udma_crc_error_count_norm', '350') #done #create section each drive given #for every drive letter listed in drives_given list, make section in drives_given: config.add_section('drive%s' % i) #write out data , close file config.write(cfgfile) cfgfile.close() print("config file created , written disk.") #check see if file healthy, if not recreate it. def check_file_integrity(): open("smartpyconfig.ini", 'r') file: if "[drivec]" in file: #not working print("found drive c in config file.") print("finished") else: print("drive c not found in config file.") create_config_file() #check config file def check_for_config(): # check see if file exists try: if cfg_path: #if cfg_path true (true = file found) print("config file found!") print("checking config file..") check_file_integrity() else: #if cfg_path not true, file not found, print("config file not found") print("creating config file.") create_config_file() except exception e: print("an exception occured, printing exception") print(e) check_for_config() the config file it's checking:
[general] logging_level = debug [standard_thresholds] threshold_value_raw_read_error_rate_norm = 101 threshold_value_reallocated_sector_count_norm = 105 threshold_value_seek_error_rate_norm = 101 threshold_value_power_on_hours_raw = 1000 threshold_value_temperature_celsius_raw = 100 threshold_value_reported_uncorrect_raw = 100 threshold_value_hardware_ecc_recovered_norm = 100 threshold_value_offline_uncorrectable_raw = 100 threshold_value_free_fall_sensor_raw = 100 threshold_value_udma_crc_error_count_norm = 350 [drivec] [drived]
your variable file file, not contents of file. may want like:
if "[drivec]" in file.read(): ... tests see if string in contents of file.
what had checks exact match on line of file, since in operator iterate on file's lines. didn't work because each line ends newline character, did not include in target string. this:
if "[drivec]\n" in file: if need match text on single line (with not whitespace on same line), work. bonus, stop finds match instead of reading whole file (although smallish files, reading whole file fast or faster).
Comments
Post a Comment