I have this code:
import talkey
tts = talkey.Talkey()
tts.say("hello world", 'en')
It outputs this to the console and plays sound when I run it:
Playing WAVE '/tmp/tmplGOau7.wav' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono
I don't want any text-output from the Talkey, is there a way to disable it?
The question is, do you want the console at all. Because if not, try to use the run the script via
pythonw.exe
instead of
python.exe
Open base.py from talkey's folder (in my case, python3 on linux mint: /usr/local/lib/python3.5/dist-packages/talkey).
Near the end find this line:
cmd = ['aplay',str(filename)]
and change it to
cmd = ['aplay','--quiet',str(filename)]
Related
I'm running a binary that manages a usb device. The binary file, when executed outputs results to a file I specify.
Is there any way in python the redirect the output of a binary to my script instead of to a file? I'm just going to have to open the file and get it as soon as this line of code runs.
def rn_to_file(comport=3, filename='test.bin', amount=128):
os.system('capture.exe {0} {1} {2}'.format(comport, filename, amount))
it doesn't work with subprocess either
from subprocess import check_output as qx
>>> cmd = r'C:\repos\capture.exe 3 text.txt 128'
>>> output = qx(cmd)
Opening serial port \\.\COM3...OK
Closing serial port...OK
>>> output
b'TrueRNG Serial Port Capture Tool v1.2\r\n\r\nCapturing 128 bytes of data...Done'
The actual content of the file is a series of 0 and 1. This isn't redirecting the output to the file to me, instead it just prints out what would be printed out anyway as output.
It looks like you're using Windows, which has a special reserved filename CON which means to use the console (the analog on *nix would be /dev/stdout).
So try this:
subprocess.check_output(r'C:\repos\capture.exe 3 CON 128')
You might need to use shell=True in there, but I suspect you don't.
The idea is to make the program write to the virtual file CON which is actually stdout, then have Python capture that.
An alternative would be CreateNamedPipe(), which will let you create your own filename and read from it, without having an actual file on disk. For more on that, see: createNamedPipe in python
I need to convert an existing short wav file to mp3 (wma would also be acceptable).
I have installed ffmpeg in the C: directory as recommended, and it works when used directly from a Windows command line, but not in Python 2.7
At first I used os.system() but although the code doesn't create an error, it does not create the mp3 file either
Using the preferred subprocess.call() gives an error message.
This one of several attempts to make it work:
import os,sys
import subprocess
# .wav file exists and contains 1 minute of audio
os.system('ffmpeg -i G:/Channel1_08.wav G:/Channel1_08.mp3')
# line above executes but does not create an mp3 file
# the same command works in a Windows command line
# ffmpeg is in the C: directory
## subprocess is supposed to be better than os.system, I tried
s = subprocess.call("ffmpeg -i G:/Channel1_08.wav G:/Channel1_08.mp3",shell = True)
# line above returns s=1, but no mp3 file
Edit: more info, I'm using a Win8.1 PC, the files are on a USB stick.
with other variations of the argument string, I often get "file not found" errors, but the file is definitely in place.
It occurs to me that I have not re-booted the PC since changing the PATH variable. I cannot do this until a major search script has finished running in about 24 hours time. I'll keep you posted.
14 Sep 17: Rebooting fixed the problem. For the record the original wav file was 2113KB, the wma is 1101KB and the mp3 is 199KB.
I want to open a new cmd window and stream some text output (logs) while my python script is running (this is basically to check where I am in the script)
I can't find w way to do it and keep it open and stream my output.
Here is what I have now:
import os
p = os.popen("start cmd", mode='w')
def log_to_cmd(process, message):
p.write(message)
for i in range(10):
log_to_cmd(p, str(i))
And I want to get 0 to 9 output on the same cmd window already open.
Thanks a lot for any suggestion.
use Baretail this software allows you to stream logs.
If you want to stick to cmd shell I'd suggest installing something like GNU Utilities for Win32. It has most favourites, including tail. tail which allows you to open file like a log veiwer .
I've got a problem with some VB scripting - it doesn't seem like it should be terribly difficult to solve, but even after trudging through many a page of Google I have yet to find a solution.
[The problem]
Here is my python file (test.py), simplified to just show the problem:
f = open("testing.txt", 'w')
f.write("oh hai\n")
f.close()
Of course, when run directly from the command line, this generates the file as you'd expect.
However, when run in a simple .vbs script (WARNING: My vbs skills are lacking. This is probably why I am having a problem. So far I haven't had many issues, apart from hating life from using XP to code when I'm used to using vim)
Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "C:\Python27\python test.py"
WshShell.Run cmd
no output file is generated! At all! It's infuriating, as when I input that exact command ("C:\Python27\python test.py") into the run program from the start menu, it works!
[System info]
At work, so they're on Windows XP. Everything else is pretty standard, or so I'm lead to believe.
EDIT: Changed "C:\Python27\testing.py" to just "testing.py". This was left over from when I was trying to solve it, and thought maybe it was putting the files somewhere outside of the destination folder.
First, your Python script looks suspicious, I doubt the backslashes work in a simple string. At least, in my test, it didn't work, I just replaced them with forward slashes.
Next, you can see the error message by prepending cmd with cmd /k, the run window remains on screen. You can see the .py file isn't found, because it isn't in the current directory. You have to specify an absolute path to this file, perhaps by getting the current path from the script.
[EDIT] I finally got a working code (my VBS is a bit rusty...)
Dim wshShell, fso, loc, cmd
Set fso = CreateObject("Scripting.FileSystemObject")
loc = fso.GetAbsolutePathName(".")
WScript.Echo loc
'~ cmd = "%ComSpec% /k C:\Languages\Python\python.exe " + loc + "\test.py"
cmd = "C:\Languages\Python\python.exe " + loc + "\test.py"
WScript.Echo cmd
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run cmd
You can also check the arguments if a path is provided:
if WScript.Arguments.Count = 0 then
loc = fso.GetAbsolutePathName(".")
else
loc = WScript.Arguments(0)
end if
Such script is better run with cscript instead of default wscript.
Try
f = open("C:\\Python27\\testing.txt", 'w')
instead of your first line.
I don't use Python very often, but I sometimes develop simple tools in it to make my life easier. My most frequently used is a log checker/crude debugger for SAS. It reads the SAS log line by line checking for any errors in my list and dumps anything it finds into standard out (I'm running Python 2.6 in a RedHat Linux environment) - along with the error, it prints the line number of that error (not that that's super useful).
What I'd really like to do is to optionally feed the script a line number and have it open the SAS log itself in GVIM and display it scrolled down to the line number I've specified. I haven't had any luck finding a way to do this - I've looked pretty thoroughly on Google to no avail. Any ideas would be greatly appreciated. Thanks!
Jeremy
Once you've got the line number, you can run gvim filename -c 12 and it will go to line 12 (this is because -c <command> is "Execute <command> after loading the first file", so -c 12 is just saying run :12 after loading the file).
So I'm not sure if you really need Python at all in this case; just sending the line number direct to gvim may be all you need.
If you want line 10
>>> f = open('thefile.log')
>>> lines = f.readlines()
>>> lines[10]
Or all at once
>>> open('thefile.log').readlines()[10]
Open in gvim
>>> import subprocess
>>> subprocess.call(['gvim', '-c', '10', 'thefile.log'])
Depending on how frequently you inspect these logs and how noisy they are, it may be worth your time to put together an errorformat so you can use Vim's quickfix list to quickly jump between errors.