Python: open cmd and stream text output - python

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 .

Related

controlling stdin and stdout for an EXE fine from within python

I have a file.exe, written by someone else, so I do not have the source code, nor do I have possibility to rewrite it. What the file.exe does is to open the ms-dos prompt, waits for 9 parameter to be provided as input via keyboard and then print text in the prompt (or in a text file if I want using file.exe > text.txt). I want to provide the imput parameter (stdin) from within a python script and read the stdout saving the output to a variable in python. Any chance to do it? I have done something simular with this instruction: os.system('wine tp52win9x.exe < inputfile.txt > res.txt') giving and writing from and to a txt file.

can I redirect the output of this program to script?

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

How to hide/disable console output from Talkey text to speech?

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)]

How to open and close a PDF file from within python

I can open a PDF file from within Python using subprocess.Popen() but I am having trouble closing the PDF file. How can I close an open PDF file using Python. My code is:
# open the PDF file
plot = subprocess.Popen('open %s' % filename, shell=True)
# user inputs a comment (will subsequently be saved in a file)
comment = raw_input('COMMENT: ')
# close the PDF file
#psutil.Process(plot.pid).get_children()[0].kill()
plot.kill()
Edit: I can close the PDF immediately after opening it (using plot.kill()) but this does not work if there is another command between opening the PDF and 'killing' it. Any help would be great - thanks in advance.
For me, this one works fine (inspired by this). Perhaps, instead of using 'open,' you can use a direct command for the PDF reader? Commands like 'open' tend to make a new process and then shut down immediately. I don't know your environment or anything, but for me, on Linux, this worked:
import subprocess
import signal
import os
filename="test.pdf"
plot = subprocess.Popen("evince '%s'" % filename, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
input("test")
os.killpg(os.getpgid(plot.pid), signal.SIGTERM)
On windows/mac, this will probably work if you change 'evince' to the path of the executable of your pdf reader.

Getting which file is open in notepad using Python

I want to figure out whether a file is open in Notepad and a File open on Adobe Reader.
If you open task manager, Go to process tab, You can see the "Command Line" column (If not, then Go to View->Select Column) which contains EXE path and opened file's path.
If I get this information, I can easily parse this string to get opened file name (Along with it's path -- Bonus!)
I found an article, which shows the way by PowerShell using WMI. Is there any way to do the same using Python 2.7
I know there's a WMI library for python but not able to figure out how to implement:
Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'" | fl *
I found a way using psutil
import psutil
for pid in psutil.pids():
p = psutil.Process(pid)
if p.name() == "notepad.exe":
print p.cmdline()

Categories

Resources