简体   繁体   中英

Validating saved text file with Python to check if parameters are set

I've got a problem with validating text file. I need to check if parameters that I set are correctly saved. File name is an actual date and time and I need to check if parameters that were send are in this text (log) file. Below you can find my code:

Arguments are sent with argpars eg. parser.add_argument("freq", type=int)


print('Saving Measurement...')
print(inst.write(':MMEMory:STORe:TRACe 0, "%s"' % timestr)) #Saving file on the inst

time.sleep(1) #Wait for file to save
print('Downloading file from device...')
ftp = FTP('XX.XX.XXX.XXX') 
ftp.login()
ftp.retrbinary('RETR %s'% timestr + '.spa', open(timestr + '.spa', 'wb').write) #downloading saved file into a directory where you run script

print('Done, saved as: ' + timestr)
time.sleep(1)

with open (timestr + '.spa') as f:
    if (str(args.freq)) in f.read():
        print("saved correctly")

ftp.delete(timestr + '.spa') #Delete file from inst
ftp.quit()

I'm not sure if it works for me. Thank you for your help

You could use the re module to help you find a date pattern inside your file. I will give you a little example code that searches, at least for this case, this date pattern dd-mm-yyyy

import re

filepath = 'your-file-path.spa'
regex = '\d\d-\d\d-\d\d\d\d'

with open(filepath, 'r') as f:
    file = f.read()

dates_found = re.findall(regex, file)
# dates_found will be an array with all the dates found in the file
print(dates_found)

You could use any regex you want as the first argument of re.findall(regex, file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM