-1

I've got watchdog and pyaudio playing together so if either of two directories is modified I hear a sound.

Now I'm trying to get a different sound for each directory. Watchdog can print the path that triggered it, so I'm trying to use that difference to fire each sound.

def on_modified(self, event,):
        x = event.src_path
        print(x)
        if x == 'c:/WATCHDOGTEST\x.csv':
            pyaudio_01.PLAY_SOUND()
        if x == 'c:/WATCHDOGTEST2\x.csv':
            pyaudio_02.PLAY_SOUND()   

The print(x) works fine:

c:/WATCHDOGTEST2\x.csv

however - the if statement won't work - I get:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \xXX escape

Any ideas appreciated!

Dan S
  • 147
  • 7

3 Answers3

1

OK just figured it out - I added a forward slash to the target directory to get rid of the backslash it added itself, now it works. Doh.

Dan S
  • 147
  • 7
1

Using \x in the string, python interprets anything after \x as hexadecimal charector, so you need to escape this charector, using one more slash. So your value will be c:/WATCHDOGTEST\\x.csv

Or you can turn it into raw string using r formater, r'c:/WATCHDOGTEST\x.csv', this is the best way, beacuse it automatically ignores any special charectory if present.

1
  • using built-in library pathlib would be more easy.
PureWindowsPath('c:/Program Files/').match(event.src_path)
KennetsuR
  • 704
  • 8
  • 17