if statement to identify directory path

Question:

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:/WATCHDOGTESTx.csv':
            pyaudio_01.PLAY_SOUND()
        if x == 'c:/WATCHDOGTEST2x.csv':
            pyaudio_02.PLAY_SOUND()   

The print(x) works fine:

c:/WATCHDOGTEST2x.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!

Asked By: Dan S

||

Answers:

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.

Answered By: Dan S

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:/WATCHDOGTESTx.csv', this is the best way, beacuse it automatically ignores any special charectory if present.

Answered By: Lokesh Kurre
Answered By: KennetsuRinn
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.