Execute Robot Framework file from python script - python

from subprocess import call
import os
call(['robot '+os.getcwd()+'\\aaa.robot'])
file_dir: D:/aaa/test/aaa.robot
script for now in same dir
Output:
Traceback (most recent call last):
File "__init.py", line 7, in <module>
call(['robot '+os.getcwd()+'\\aaa.robot'])
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 710, in __init_
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execut
startupinfo)
WindowsError: [Error 2] Nie mo┐na odnalečŠ okreťlonego pliku
I just cant handle it. I dont get why in python tunning anything is so complicated :(
I want same result as this line (written directly to cmd):
/>robot aaa.robot

Here is another way
import robot
logFile = open('mylog.txt', 'w')
robot.run("tmp.robot",stdout=logFile)

subprocess expects a list but you're inputting a string ('robot '+os.getcwd()+'\\aaa.robot').
Try:
call(['C:/Python27/python.exe', '-m', 'robot', 'D:/aaa/test/aaa.robot'])
or
call(['C:/Python27/Scripts/robot.bat', 'D:/aaa/test/aaa.robot'])

Related

subprocess.Popen; FileNotFoundError :[WinError 2]. The system cannot find the file specified

I am working on a script to analysis proteins. One of the def if for creating a makeblastdb, and then use it to carry out blast. The code is as below:
import subprocess
from Bio import SeqIO
from Bio.Blast.Applications import NcbiblastnCommandline
def blast_file(input_data_files, db_files, output):
input_data_files = opts.input_file + '/protein_extracted_file.fa'
db_files = opts.input_file +'/alleles_reference_db'
cmd = ["makeblastdb", "-in", db_files, "-dbtype", "prot", "-parse_seqids", "-out", output + "/temporary_db",
"-title", "temporary_db"]
my_file = subprocess.Popen(cmd)
my_file.wait()
blasttn_cline = NcbiblastnCommandline(query=input_data_files, subject=db_files, evalue=0.001, outfmt=5,
out=output + '/blast_output.xml')
blasttn_cline()
The script works well until it get to the above def, and I get the error message:
Traceback (most recent call last):
**> File "open.py", line 234, in <module>
> main() File "open.py", line 229, in main
> blast_file(input_data_files, db_files, output) File "open.py", line 165, in blast_file
> my_file = subprocess.Popen(cmd)
File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 709, in __init__
> restore_signals, start_new_session) File "C:\ProgramData\Anaconda3\lib\subprocess.py", line 997, in
> _execute_child
> startupinfo) FileNotFoundError: [WinError 2] The system cannot find the file specified**
How can I make this to work for python3? Originally the script was written in python2.7, and I have updated most of it but I am stuck in this area. Any suggestion to update it?

Execute externall compiled program in Python

I would like to execute an external compiled program in Python. In shell,
./bin/myProgram ./dir1/arg1 ./dir/arg2 arg3 arg3 arg4
And my Python script looks like:
import subprocess
subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg2",
"arg3", "arg4", "arg5"])
But I get this error:
Traceback (most recent call last):
File "/Users/Solar/Desktop/test.py", line 5, in <module>
"32", "0.06", "15"])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Process finished with exit code 1
Could you help me? Thank you in advance!!
It's solved, I just had to execute
subprocess.call(["/Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
...
Instead of
subprocess.call(["./Users/Solar/Desktop/parent/child1/child2/bin/myProgram",
"/Users/Solar/Desktop/parent/child1/child2/dir1/arg1",
...
Thank you people!!
when subprocess.Popen issues
OSError: [Errno 2] No such file or directory
that's only because the executable cannot be found (if one of the file-arguments was not found it's the sub-process which reports the error, and no exception is thrown)
In your case ./Users/... is not likely to be valid considering that /Users/ is valid (root against current dir) so that's your problem.
"./Users/Solar/Desktop/parent/child1/child2/bin/myProgram" should be "/Users/Solar/Desktop/parent/child1/child2/bin/myProgram"
Note: better coding could have avoided the error:
import subprocess
root = "/Users/Solar/Desktop/parent/child1/child2"
subprocess.call([os.path.join(root,"bin/myProgram"),
os.path.join(root,"dir1/arg1"),
os.path.join(root,"dir1/arg2"),
"arg3", "arg4", "arg5"])

run a .exe file with additional parameters

I am using windows and struggeling to get this work...
I can execute this in cmd.exe:
"C:\Program Files (x86)\Test 123\Test.exe" "H:\Test Test\file.txt" -f "doStuff"
but when I try to do it in python:
subprocess.call([r'"C:\Program Files (x86)\Test 123\Test.exe" "H:\Test Test\file.txt" -f "doStuff"'])
I get this error:
Traceback (most recent call last):
File "testing12.py", line 20, in <module>
subprocess.call([r'"C:\Program Files (x86)\Test 123\Test\Test.exe" "H:\Test Test\Folder\file.txt" -f "doStuff"'])
File "c:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "c:\Python27\lib\subprocess.py", line 709, in __init__
errread, errwrite)
File "c:\Python27\lib\subprocess.py", line 957, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
How can I execute it properly? Thanks.
If you're going to pass in an array, make it an actual array -- one argument per parameter, separated by commas. Otherwise you'll need to use shell=True, which has all the (generally undesirable) side effects of invoking a shell (and should just pass in your command string as a string, no array called for in that use case).
subprocess.call([
"C:\Program Files (x86)\Test 123\Test.exe",
"H:\Test Test\file.txt",
"-f", "doStuff"])
If you don't use commas between your strings, they're consolidated together.

Error using subprocess.check_output in Python

I am at a very basic level in Python, and I'm trying to learn how to use the subprocess module. I have a simple calculator program called x.py that takes in a number, multiplies it by 2, and returns the result. I am trying to execute that simple program from IDLE with the following two lines of code, but I get errors. The number 5 is the number I'm trying to feed into x.py to get a result. Would someone mind helping me understand what I'm doing wrong and help me get it right? Thanks!
import subprocess
result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
result = subprocess.check_output(["C:\\Users\\Kyle\\Desktop\\x.py",5])
File "C:\Python27\lib\subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 855, in _execute_child
args = list2cmdline(args)
File "C:\Python27\lib\subprocess.py", line 587, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
Pass 5 as a string:
import sys
result = subprocess.check_output([sys.executable, "C:\\Users\\Kyle\\Desktop\\x.py", '5'])

NamedTemporaryFile cannot be acessed from command line

I have the following (simplified) code:
with NamedTemporaryFile() as f:
f.write(zip_data)
f.flush()
subprocess.call("/usr/bin/7z x %s" % f.name)
It dies with the following error:
Traceback (most recent call last):
File "decrypt_resource.py", line 70, in <module>
unpack(sys.argv[2])
File "decrypt_resource.py", line 28, in unpack
print(subprocess.check_output(cmd))
File "/usr/lib/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
However, if I use NamedTemporaryFile(delete=False) and then print & execute the command, it works. What's wrong here?
My System is an ArchLinux with a 3.9.5-1-ARCH kernel.
You are using subprocess.call() incorrectly.
Pass in a list of arguments:
subprocess.call(["/usr/bin/7z", "x", f.name])
The argument is not handled by a shell and is not parsed out like a shell would do. This is a good thing as it prevents a security problem with untrusted command line arguments.
Your other options include using shlex.split() to do the whitespace splitting for you, or, as a last resort, telling subprocess to use a shell for your command with the shell=True flag. See the big warning on the subprocess documentation about enabling the shell.

Categories

Resources