-1

The short script below is a part of a Nikon camera control script on my rasberry PI 4 to control long exposures of my Nikon D90. I call a command that tests if the camera is communicating, the result is in variable mycmd. The script below I just create the string. But my if statement always fails to find a match on less Nikon is the only word in mycmd.

import os
import time
import subprocess
import sh
mycmd =("Nikon DSC D90 (PTP mode)       usb:001,008")
if mycmd == ('Nikon'):
    print('gotit')


#mycmd = os.popen('gphoto2 --auto-detect').read()
print(mycmd)
time.sleep(1)
#print(encoded_bytes)
#decoded_string = encoded_bytes.decode('utf-8','replace')
#print(decoded_string)

if mycmd == ('Nikon'):
    print("OK")
else:
    print("error")`
The OUTPUT Below
>> %Run os.popen_example.py
Nikon DSC D90 (PTP Mode)     usb:001,008
error
>>
dwb
  • 2,136
  • 13
  • 27
  • 1
    Are you trying to check if `mycmd` *contains* the word `"Nikon"`? Because at the moment, by using `==`, you are checking is `mycmd` *exactly equals* `"Nikon"`. To see if it *contains* it, try using `"Nikon" in mycmd` – dwb Apr 06 '21 at 17:13

1 Answers1

0

If "Nikon" might not be the first word, try this:

mycmd = os.popen('gphoto2 --auto-detect').read()
print(mycmd)
time.sleep(1)

if 'Nikon' in mycmd:
    print("OK")
else:
    print("error")

Otherwise, if you expect that "Nikon" should always be the first token, try this instead:

mycmd = os.popen('gphoto2 --auto-detect').read()
print(mycmd)
time.sleep(1)

if mycmd.startswith('Nikon'):
    print("OK")
else:
    print("error")`
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85