Python Forum
Short code for EventGhost not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Short code for EventGhost not working
#1
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
nilamo write Feb-08-2021, 09:53 PM:
Please use code tags in the future, to preserve whitespace. I've added them for you this time.
Reply
#2
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.
Reply
#3
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/*.*'
Reply
#4
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).
Reply
#5
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)
Reply
#6
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.
Reply
#7
(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
Reply
#8
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?
Reply
#9
I got it working thanks for all your help
nilamo likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My Code isn't working... End3r 4 625 Mar-21-2022, 10:12 AM
Last Post: End3r
  Illegal instruction? working code for months? korenron 4 4,797 Aug-05-2021, 09:57 AM
Last Post: korenron
  I don't undestand why my code isn't working. RuyCab 2 1,221 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 1,622 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Code no longer working yk303 14 5,418 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  How can I make a short-key in Spyder (Python IDE)? moose 3 1,643 Nov-02-2020, 12:13 PM
Last Post: jefsummers
  autocomplete working code sample not working... aviper4u 0 1,118 Oct-24-2020, 03:04 AM
Last Post: aviper4u
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 2,127 Oct-05-2020, 07:03 PM
Last Post: buran
  Help me get this image converter code working? NeTgHoSt 0 1,294 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Working code to build GPX from geotagged JPGs? Winfried 3 1,646 May-10-2020, 08:58 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020