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!
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.
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.
- using built-in library pathlib would be more easy.
PureWindowsPath('c:/Program Files/').match(event.src_path)
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!
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.
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.
- using built-in library pathlib would be more easy.
PureWindowsPath('c:/Program Files/').match(event.src_path)