How to execute an external execution file with parameters in Python 3? - python-3.x

I want to execute an exe file using Python 3.4.
That is,
C:/crf_test.exe -m input.txt output.txt
When I executed this at the command line, the result was:
Go SEARCH
to O
...
But, when I executed this in Python like this:
import os
os.startfile('crf_test.exe -m model.txt test.txt')
Nothing happened (I mean appeared in the result window.)

Using os.popen() you can execute and read commands:
cmd = os.popen(r'crf_test.exe -m model.txt test.txt')
result = cmd.read()

Related

os.system(cmd) call fails with redirection operator

My Python 3.7.1 script generates a fasta file called
pRNA.sites.fasta
Within the same script, I call following system command:
cmd = "weblogo -A DNA < pRNA.sites.fasta > OUT.eps"
os.system(cmd)
print(cmd) #for debugging
I am getting the following error message and debugging message on the command line.
Error: Please provide a multiple sequence alignment
weblogo -A DNA < pRNA.sites.fasta > OUT.eps
"OUT.eps" file is generated but it's emtpy. On the other hand, if I run the following 'weblogo' command from the command line, It works just find. I get proper OUT.eps file.
$ weblogo -A DNA<pRNA.sites.fasta>OUT.eps
I am guessing my syntax for os.system call is wrong. Can you tell me what is wrong with it? Thanks.
Never mind. It turned out to be that I was not closing my file, "pRNA.sites.fasta" before I make system call that uses this file.

isatty() always returning False?

I want to pipe data via stdin to a Python script for onwards processing. The command is:
tail -f /home/pi/ALL.TXT | python3 ./logcheck.py
And the Python code is:
import sys
while 1:
if (sys.stdin.isatty()):
for line in sys.stdin:
print(line)
I want the code to continuously watch stdin and then process each row when received. The tail command is working when run on its own but the python script never outputs anything.
Checking isatty() it appears to always return False?
Help!
A TTY is when you use your regular terminal - as in opening up a python in your shell, and typing
BASH>python
>>>from sys import stdin
>>>stdin.isatty() #True
In your case the standard input is coming from something which is not a tty. Just add a not in the if statement.

How to write command using subprocess in python3

import subprocess
proc = subprocess.Popen(['systemctl', 'reload', 'postgresql-9.6.service'], stdout=subprocess.PIPE, shell=True)
(db_cmd, err) = proc.communicate()
print (db_cmd)
I am trying to run systemctl reload postgresql-9.6.service using python 3 but I am not able to get output. And I am getting output such as:
reload: systemctl: command not found
b''
First of all: read the docs: Subprocess module Python 3.
You mistyped the import statement: it should be: "import subprocess".
Use sudo to execute the program you wrote: sudo python /full/path/to/your/script.
Then: it is more pythonic if you write db_cmd = proc.communicate()[0] because in this way you create only the variable you use.
Finally, your error is indicating that something went wrong while you were processing the systemctl command. In particular it seems to be missing the reload command. Try using restart.
In addition: this question is a duplicate of How to use subprocess.

python script to capture output of top command

I was trying to capture output of top command using the following python script:
import os
process = os.popen('top')
preprocessed = process.read()
process.close()
output = 'show_top.txt'
fout = open(output,'w')
fout.write(preprocessed)
fout.close()
However, the script does not work for top. It gets stuck for a long time. However it works well with commands like 'ls'. I have no clue why this is happening?
Since you're waiting for the process to finish, you need to tell top to only print its output once, and then quit.
You can do that by running:
top -n 1
-b argument required when stdout read from python
os.popen('top -b -n 1')
top -b -n 1

MATLAB executing command line scripts for starting C++ programs? (without MEX)

I am running MATLAB 2011a under Ubuntu, and I have some C++ functions I execute from the command line such as `./community sample_networks/karate.bin -l -1 -q 0.01 > sample_networks/karateout.txt' These C++ functions produce a text file which I would like to pick up from MATLAB
I have not written these C++ functions and would like to simply have MATLAB pass a string to the command line to be executed so that the text file result can be picked up from MATLAB. I would like to avoid using MEX for the time being.
EDIT (using the system command does not work):
pwd
ans = /home/alex/Documents/MATLAB/MATLABsvnWorkingDir/Bloom/graphAnalysis/analysisAttempt2/functionsDownloaded/BlondelLouvainCPP/Community_BGLL_CPPLinux
system('./community sample_networks/karate.bin -l -1 -q 0.01 > sample_networks/karateout.txt > sample_networks/karateout.txt')
./community: /home/alex/matlab2011a/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by ./community)
ans = 1
Looks like you just need to use the system function. This function will launch another executable, and wait until its finished.

Resources