1

Sorry if this is a simple question and has been answered before, but I couldn't find it anywhere.

I'm trying to listen to UDP packets and if they are certain packets, run different batch scripts. I have this working correctly, but I have found that if the Popen command doesn't find the file it triggers an exception and the script stops running. Ideally, I want this to print a message and then continue listening for other packets and act upon them, just giving us a message for debugging. Here is the code I have used, how could I do this?

if rxdata == "Camera 1":
    from subprocess import Popen
    try:
        p = Popen("Camera 1.bat", cwd=r"C:\xxx")
        stdout, stderr = p.communicate()
    except FileNotFoundError():
        print('Camera 1.bat not found')
elif rxdata == "Camera 2":
    from subprocess import Popen
    p = Popen("Camera 2.bat", cwd=r"C:\xxx")
    stdout, stderr = p.communicate()

In both examples, I receive the following and the script closes.

Traceback (most recent call last):
   File "C:\UDP Listener.py", line 42, in <module>
     p = Popen("Camera 1.bat", cwd=r"C:\xxx")
   File "C:\Python34\lib\subprocess.py", line 858, in __init__
     restore_signals, start_new_session)
   File "C:\Python34\lib\subprocess.py", line 1111, in _execute_child
     startupinfo)
   FileNotFoundError: [WinError 2] The system cannot find the file specified

Thanks in advance

Matt

6
  • Just put the call in a try/except block. See here. Commented Jun 25, 2014 at 14:24
  • Camera 1 has a try / except block, but I receive the same message. Am I doing something wrong? Commented Jun 25, 2014 at 14:31
  • 1
    except FileNotFoundError(): is wrong. You should not give a (newly created) instance of the exception here, but the exception class itself. Commented Jun 25, 2014 at 14:35
  • @glglgl Something is fishy here; running something like try: raise FileNotFoundError except FileNotFoundError() results in TypeError: catching classes that do not inherit from BaseException is not allowed. That the OP is not getting this exception could indicate that the call inside the try block does not get executed. Commented Jun 25, 2014 at 14:36
  • @user4815162342 since the traceback contains Popen("Camera 1.bat", that code gets executed. Weird, maybe he even used Python 2 for generating this traceback without knowing. Commented Jun 25, 2014 at 14:50

2 Answers 2

1

You must not use the brackets behind the FileNotFoundError (don't call it, just "name" it). Test (with Python 2):

try:
    b = a
except NameError():
    print "NameError caught."

Execution:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    b = a
NameError: name 'a' is not defined

For instance, OSError is a type, whereas OSError() creates an instance of this type:

>>> type(OSError)
<type 'type'>
>>> type(OSError())
<type 'exceptions.OSError'>
Sign up to request clarification or add additional context in comments.

7 Comments

That's not how it works in Python 3, which the OP uses. What you get there is NameError: name 'a' is not defined; During handling of the above exception, another exception occurred: TypeError: catching classes that do not inherit from BaseException is not allowed
I see, the symptoms are different, but the origin of the problem is the same, right?
I have just tried this with removing the () and receive the same error.
@matth9 then please take great care of the line numbers provided in the traceback and see if the error is triggered by those lines that you were showing here.
try: is line 42 and p = Popen("Camera 1.bat", cwd=r"C:\xxx") is line 43
|
1

Strangely, after re-installing python on my PC everything is now working correctly. Not sure what went wrong but when I run the code now and an exception is found then the code prints as expected.

Thanks for your help!

Comments