I'm still new to Python firstly.
I am trying to, (whilst using Zmodem in Command Prompt) read a .txt file for information, then, if certain strings are found within the .txt file, send instructions to Zmodem using pyautogui.Keybaord.Keys.
I am aware that this may not be the 'best' or most 'efficient' approach, however as I said I'm still learning and am aware that I need to learn by experience, which I'm fine with. I'd rather be good at the basics and then learn why it's best not to do something a certain way. My real issue here is that for the life of me I can't work out why exactly this isn't working.
My gut tells me that either I am not reading the .txt file for the specified content correctly, OR that somehow the pyautogui.Keyboard_Keys instructions aren't being sent to Command Prompt.
I am expecting the file to be read, parameters checked against the content inside the .txt file and then, depending on what it finds, send the appropriate string to Zmodem. All works fine until the With section..
The contents of the .txt file is as follows, and is UTF-8:
V100
Boot time: 2000/01/01 00:00:00
Firmware: 2.1.12.7
Status: 0
Hardware: 4.01
`os.system('start cmd')
time.sleep(1)
print('\nSetting up Command Prompt for ZmTest')
pyautogui.KEYBOARD_KEYS
pyautogui.press('I')
pyautogui.press(':')
pyautogui.press('return')
time.sleep(0.6)
pyautogui.typewrite('cd I:\VT\ZmodemTest')
pyautogui.press('return')
time.sleep(0.6)
pyautogui.typewrite('ZmTest.exe')
pyautogui.press('return')
time.sleep(0.6)
print('\nZmTest has been set up. Beginning now...')
time.sleep(0.6)
print('\nNow restoring original firmware version. Please wait...')
with open('I:\\VT\\N Testing\\Config\\BOOT.TXT', 'r') as file:
content = file.readline()
type_100 = ('V100')
type_50 = ('V50')
V10X = ('Hardware: 4.01')
V10E = ('Hardware: 5.01')
version_21127 = ('Firmware: 2.1.12.7')
for content in file:
if type_100 + V10E + version_21127 in content:
pyautogui.typewrite('ZmTest.exe -port:4 -send I:\\VT\\firmware\\2.1.12.7\\E\\vware.dat')
pyautogui.press('return')
time.sleep(0.6)
break
elif type_100 + V10X + version_21127 in content:
pyautogui.typewrite('ZmTest.exe -port:4 -send I:\\VT\\firmware\\2.1.12.7\\X\\fware.dat')
pyautogui.press('return')
time.sleep(0.6)
break
time.sleep(50)
print('all done')`
-- readline only reads one line. You want either
read
onreadlines
. The former with get you the file's contents in a single string (including line breaks). The latter will give you a list of lines (including empty lines).-- You overwrite your
content
variable with your for loop. Open an intercative python session in a terminal you try this:With the
for
loop we iterate over something (that something is called an iterable) that contains some items. Thefor
loop declares each item's name inside the loop.--
type_100 + V10E + version_21127
concatenates these stings exactly, the result isV100Hardware: 4.01Firmware: 2.1.12.7
. None of your files will have this exact string.The general advice here is open an interactive session of python and get familiarised with the basics. You need to know what your objects are and what they contain to search for their contents and to compare them.