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

matth9
  • 21
  • 3
  • Just put the call in a `try`/`except` block. See [here](https://docs.python.org/3.4/tutorial/errors.html#handling-exceptions). – dano Jun 25 '14 at 14:24
  • Camera 1 has a try / except block, but I receive the same message. Am I doing something wrong? – matth9 Jun 25 '14 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. – glglgl Jun 25 '14 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. – user4815162342 Jun 25 '14 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. – Dr. Jan-Philip Gehrcke Jun 25 '14 at 14:50
  • @Jan-PhilipGehrcke Yeah, the `Camera 1` code path gets executed, but maybe it's an older version of the code that is not in the `try` block. I also considered that it was Python 2, but Python 2 doesn't have `FileNotFoundError`. – user4815162342 Jun 25 '14 at 15:00

2 Answers2

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'>
Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • 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` – user4815162342 Jun 25 '14 at 14:38
  • I see, the symptoms are different, but the origin of the problem is the same, right? – Dr. Jan-Philip Gehrcke Jun 25 '14 at 14:39
  • I have just tried this with removing the () and receive the same error. – matth9 Jun 25 '14 at 14:50
  • @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. – Dr. Jan-Philip Gehrcke Jun 25 '14 at 14:51
  • `try:` is line 42 and `p = Popen("Camera 1.bat", cwd=r"C:\xxx")` is line 43 – matth9 Jun 25 '14 at 14:57
  • That does not help us. Either provide a minimal working example that we can test or provide more details. In any case, you are not making proper use of Python's exception handling in all places of your code. – Dr. Jan-Philip Gehrcke Jun 25 '14 at 15:02
  • @matth9 But according to the traceback you posted, `p = Popen("Camera 1.bar", ...)` is on line 42. Are you editing the correct file? You can test it by replacing the `Popen` invocation with something nonsensical like `1/0`. – user4815162342 Jun 25 '14 at 15:02
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!

matth9
  • 21
  • 3