Using aplay to play a sound through subprocess within a python script - python

There are alot of libraries to play audio within a python script, I was wondering if it would be possible to simply use call aplay through the subprocess feature to play a sound? When I try it I get OSError: [Errno 2] No such file or directory but there is definitely a sound there, it works when I do it through the command prompt. I may be doing something wrong as far as syntax in the python script?
from subprocess import call
call(["aplay /home/pi/file.wav"])

The syntax that will work is :
from subprocess import call
call(["aplay", "/home/pi/file.wav"])

I found that installing
alsa-utils
in this case : sudo apt install alsa-utils
make it work.
example of "text to speech"
import pyttsx3
# init function to get an engine instance
engine = pyttsx3.init()
# say method for input text to be spoken
engine.say('Here the message you want you hear')
# run and wait method, it processes the voice commands.
engine.runAndWait()
I hope it helps.

Related

Why does the "which" system command give a 256 code with os.system in python?

I am on mac OSX.
I have a program where I am trying to call downloaded libraries from the terminal. This is not possible if I don't know where the libraries are. I will use pip as a common library example
>>> os.system("pip -h")
32512
>>> os.system("which pip")
256
I have read this response to the 256 error, however, I still don't understand why it appears here. It says it is "frequently used to indicate an argument parsing failure" however the exact command works because this does not seem to be an argument parsing error to me.
I would like to be able to do something to the effect of:
os.system(os.system("which pip") +" -h")
If there is another way of doing this, I would love to hear it
Don't use os.system like that (and don't use which, either). Try this to find a program:
import os
for bin_dir in os.environ.get("PATH").split(":"):
if 'my_program' in os.listdir(bin_dir):
executable_path = os.path.join(bin_dir, 'my_program')
break
Note that this does assume that PATH was properly set by whatever process started the script. If you are running it from a shell, that shouldn't be an issue.
In general, using os.system to call common *NIX utilities and trying to parse the results is unidiomatic-- it's writing python as if it was a shell script.
Then, instead of using system to run pip, use the solution describe in this answer.

How to run an autohotkey script in Python (2.7)

I'm trying to run an autohotkey (ahk) script in Python 2.7 but nothing seems to work. All online sources I've found are either outdated or overly complicated.
Has anyone found a way of doing this? I just want to run a couple of simple scripts that activates windows and opens applications. E.g:
IfWinExist, Command Prompt - python ...
WinActivate
Update:
I've tried downloading pyahk:
ahk.start() # Ititializes a new script thread
ahk.ready() # Waits until status is True
ahk.execute(mw._['cwd']+"winActivate_cmd.ahk") # Activate cmd window
error: can't load autohotkey.dll
as well as trying this:
import win32com.client # Import library / module
dll = win32com.client.Dispatch("AutoHotkey.Script") #Creating DLL object?
dll.ahktextdll() #no idea what this is doing...
dll.ahkExec("WinActivate, Command Prompt - python")
pwintypes.com_error invalid class string
It seems like you should be able to just launch autohotkey with the script as a parameter using subprocess:
subprocess.call(["path/to/ahk.exe", "script.ahk"])
You'd have to check the autohotkey docs but this seems like it ought to work.

Lock windows workstation using Python

Is there a way to lock the PC from a Python script on Windows?
I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.
This can be done with the LockWorkStation() function from user32.dll:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
In Python it can be called using using the ctypes/windll FFI from the Python stdlib:
import ctypes
ctypes.windll.user32.LockWorkStation()
A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell.
try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!!
so we can use subprocess to run this command like this:
import subprocess
cmd='rundll32.exe user32.dll, LockWorkStation'
subprocess.call(cmd)
One more solution :
Run command to install the necessary package
pip install pyautogui
Run the below code
import pyautogui
from time import sleep
pyautogui.hotkey('win', 'r')
pyautogui.typewrite("cmd\n")
sleep(0.500)
pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")

Python os.system launch exe with quotes and dash arguments

Good Day all
I am trying to run the following command, but receive various error's I know I have the incorrect syntax or possibly using an incorrect method, would any one be kind enough to point in the correct direction.
Thanks for any assistance
the actual external program path as in windows command line or batch script would be.
"c:\Program Files\SQL Anywhere 11\Bin32\dbbackup.exe" -c "DSN=demo2suite;UID=dba;PWD=sql" -y "D:\Databases\demo2\LIVE\LIVE_BCK"
Python V3.3
my part of the code for this mini(newbie) project would be.
def BackupDatabase():
try:
os.system('c://Program Files//SQL Anywhere 11//Bin32//dbbackup.exe -c "DSN=amos2suite;UID=dba;PWD=sql" -y "D://Databases//AMOS2//LIVE//LIVE_BCK"')
except OSError as e:
WriteLog("error",e)
It's better to use subprocess module.
Something like this:
import subprocess
subprocess.call(['c://Program Files//SQL Anywhere 11//Bin32//dbbackup.exe','-c',
'"DSN=demo2suite;UID=dba;PWD=sql"', '-y','"D://Databases//AMOS2//LIVE//LIVE_BCK"'])

Callback when VLC done on command line

I need to make VLC download then play songs. I'm planning on using the os.popen to issue commands to the VLC command line (I'm having some problems getting the python binding working...). My question is, is there any callback that I can get when VLC is done downloading so that I can know to start streaming?
You're probably better off doing the download yourself, but if you really want to do this, you might be able to combine http://stromberg.dnsalias.org/~strombrg/notify-when-up2.html with a sniffer like tshark or tcpdump.
Or... You could modify vlc. ^_^
To close VLC after any actions append vlc://quit to your command line

Categories

Resources