Python Forum
Short code for EventGhost not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Short code for EventGhost not working (/thread-32420.html)



Short code for EventGhost not working - Patricia - Feb-08-2021

Hi Guys. I'm just starting to learn Python
I have a short script that is supposed to play a random wav file in a folder but only between 9 and 5
It returns an error and I'm not sure what I have done wrong
from os.path import isfile, join
from os import listdir
import random
import datetime

hr = datetime.datetime.now().hour

if hr < 9 or hr > 17: 
  exit()

#path you want to get wav files from
path = "C:\Sounds\Clock Sounds for EG\Doorbell"
onlyfiles = [ f for f in listdir(path) if isfile(join(path,f)) ]

onlywavfiles = []
for f in onlyfiles:
    if f[-3:] == "wav":
        onlywavfiles.append(f)

#generate random number based on number of available files
randomnum = random.randint(0,len(onlywavfiles)-1)

eg.plugins.System.PlaySound(path + "/" + onlywavfiles[randomnum], 1, False)
This is the error
Error:
Traceback (most recent call last): Python script "9", line 9, in <module> exit() NameError: name 'exit' is not defined
Thanks Patricia


RE: Short code for EventGhost not working - nilamo - Feb-08-2021

exit() doesn't exist normally (it does when using the interactive prompt, to make it easier to quit it). If you want to use it, add import sys to the top of your file, and then call sys.exit() instead.


RE: Short code for EventGhost not working - Patricia - Feb-09-2021

Thanks I made the correction but
When the time is outside of 9 and 16 the script runs (no errors and does not play any wav file)
When the time is inside of 9 and 16 I get an error
import sys
from os.path import isfile, join
from os import listdir
import random
import datetime

hr = datetime.datetime.now().hour

if hr < 9 or hr > 16: 
  sys.exit()

#path you want to get wav files from
path = "C:\windows\test"
onlyfiles = [ f for f in listdir(path) if isfile(join(path,f)) ]

onlywavfiles = []
for f in onlyfiles:
    if f[-3:] == "wav":
        onlywavfiles.append(f)

#generate random number based on number of available files
randomnum = random.randint(0,len(onlywavfiles)-1)

eg.plugins.System.PlaySound(path + "/" + onlywavfiles[randomnum], 1, False)
Error:
Traceback (most recent call last): Python script "27", line 14, in <module> onlyfiles = [ f for f in listdir(path) if isfile(join(path,f)) ] WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\windows\test/*.*'



RE: Short code for EventGhost not working - deanhystad - Feb-09-2021

It shows the result of join as 'C:\\windows\test/*.*'. Does that look correct to you? os.join() added the '/' between test and *.*, and if you look at the os.join() documentation it says:
Quote:Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last
This indicates that '/' is the directory separator.

You could change path to be 'C:/windows/test'. I think you could also use 'C:\\windows\\test' or r'C:\windows\test'. I believe your problem is that '\' is the escape character in a string and it is interpreting \t as a tab. There is no \w, so join is adding an additional backslace ('\\' is how you make a backslash character in a string).


RE: Short code for EventGhost not working - Patricia - Feb-09-2021

my mistake I was using the wrong path (c:\windows\test Should have been C:\test Sorry about that
The script runs now but If I change the time 16 to 11 (the time here is 11:10 the script still runs and plays a wav file
The wav file should only play between 9 and 11 (that's the way I read it)

import sys
from os.path import isfile, join
from os import listdir
import random
import datetime
 
hr = datetime.datetime.now().hour
 
if hr < 9 or hr > 11: 
  sys.exit()
 
#path you want to get wav files from
path = "C:\Test"
onlyfiles = [ f for f in listdir(path) if isfile(join(path,f)) ]
 
onlywavfiles = []
for f in onlyfiles:
    if f[-3:] == "wav":
        onlywavfiles.append(f)
 
#generate random number based on number of available files
randomnum = random.randint(0,len(onlywavfiles)-1)
 
eg.plugins.System.PlaySound(path + "/" + onlywavfiles[randomnum], 1, False)



RE: Short code for EventGhost not working - nilamo - Feb-09-2021

A backslash is an escape character, that's why it looks weird in the error message. You can either escape them, use a raw string, or use forward slashes.

path1 = "c:\\windows\\test"
path2 = r"c:\windows\test"
path3 = "c:/windows/test"
Getting in the habit of using forward slashes will help when you start writing scripts to work in more places than just windows.


RE: Short code for EventGhost not working - nilamo - Feb-09-2021

(Feb-09-2021, 04:13 PM)Patricia Wrote: The wav file should only play between 9 and 11 (that's the way I read it)

11 isn't greater than 11.

>>> current_hour = 11
>>> current_hour > 11
False
>>> current_hour >= 11
True



RE: Short code for EventGhost not working - deanhystad - Feb-09-2021

In other words, 11:10 is <= 11 if all you are comparing is hours. Reminds me of the Kanye West ballot debate in Wisconsin. Is 5:00.59 still 5 o'clock?


RE: Short code for EventGhost not working - Patricia - Feb-09-2021

I got it working thanks for all your help