I'm trying to capture the results of my logging to do a diff in the end and verify the results are what as expected on Robot test. I've tried adding the following:
stdout=/path/to/file however that only seems to print python 'print()' statements and doesn't actually utilize my loggers. I was wondering, if I do the following:
Test Case
Start Process python ../Scripts/test.py
How do I get the logs produced by test.py in a separate file?
You can always run the proces in shell and just redirect output of the command. Like so
Process.Start Process python3 ../Scripts/test.py > ../Scripts/test.log shell=yes alias=test
I would also suggest to use ${CURDIR}. That way you can execute your robot from different locations and it will still work.
Related
I am trying to execute this bash command n python and try to evaluate the output, based on the output, I want to show my own-defined outputs.
The bash command I am trying to execute using the python is:
kubectl get pods --field-selector=status.phase=Failed -n kube-system
everything looks really good and only problem I am having is,
This outputs No resources found, means there are no resources matching the given criteria (i.e status.phase=Succeeded), but that is fine, but the problem is It prints in the terminal. What I want to do is, prints my own-defined output when the actual output is No resources found, but I can't do that since it already prints that output in the terminal. I even can't use the status_code to check, it always gives 0 even after the resources are found or not (which is correct) since code has executed successfully. Is there a way that I can filter the output during the executing of the bash command and gives a condition based on the output to print my own-defined output?
Here is my code snippet:
def subprocess_execute_arr(self):
output = subprocess.call(self)
return output
cmd_failed = ["kubectl", "get", "pods", "--field-selector=status.phase=Failed", "-n", "kube-system"]
failed = execute.subprocess_execute_arr(cmd_failed) //this is where it prints the output in the terminal
output:
No resources found.
PS: Here output is not an error, command has executed correctly but I don't need to print this output.
Any idea how to solve this?
I have two script
1. demo.ksh
2. demo.py
in demo.ksh i am exporting variable as
#!/bin/ksh
TEST="Hello"
export TEST
in demo.py I am executing demo.ksh and trying to read exported value as ..
import os
import subprocess
cmd='. demo.ksh' #I even tried 'demo.py' (no .)
subprocess(cmd,shell=True,stdout=subprocess.PIPE)
print(os.getenv('TEST'))
print(os.environ['TEST'])
I am expecting
Hello
Hello
But getting
None
KeyError: 'TEST'
Although it is a simple exercise. I could not find correct solution for this.Please help me what is wrong with my code.
Exporting a variable makes it available to sub-processes of the spawned shell process, but not to the parent process (i.e. your Python program).
To get the expected output try a shell script like this:
#!/bin/ksh
TEST="Hello"
export TEST
python demo.py
You can instead communicate with the subprocess via STDOUT. For this, subprocess.check_output can be useful.
I got a python file which is a code that I developed. During his execution I input from the keyboard several characters at different stages of the program itself. Also, during the execution, I need to close a notepad session which comes out when I execute into my program the command subprocess.call(["notepad",filename]). Having said that I would like to run this code several times with inputs which change according to the case and I was wondering if there is an automatic manner to do that. Assuming that my code is called 'mainfile.py' I tried the following command combinations:
import sys
sys.argv=['arg1']
execfile('mainfile.py')
and
import sys
import subprocess
subprocess.call([sys.executable,'mainfile.py','test'])
But it does not seem to work at least for the first argument. Also, as the second argument should be to close a notepad session, do you know how to pass this command?
Maybe have a look at this https://stackoverflow.com/a/20052978/4244387
It's not clear what you are trying to do though, I mean the result you want to accomplish seems to be just opening notepad for the sake of saving a file.
The subprocess.call() you have is the proper way to execute your script and pass it arguments.
As far as launching notepad goes, you could do something like this:
notepad = subprocess.Popen(['notepad', filename])
# do other stuff ...
notepad.terminate() # terminate running session
I have a Python project with some tests in different folders and files. I wanted to write a script which executes all tests for the project and outputs some results. I want it to be a ruby script (because I know Ruby more than Python and for now I enjoy it more) and I was thinking to get the output from the tests and to parse them with ruby and to output something like "48 tests run in total, all ok" instead of the the output from python.
Long story short - I want I way to get the output from python test_something.py in a variable or in a file and I want nothing from it on the screen.
Here are my tries:
tests = Dir.glob("**/test_*")
wd = Dir.pwd
output = ''
tests.each do |test|
Dir.chdir(File.dirname(test))
# output += `python #{File.basename(test)}`
# system("python #{File.basename(test)} >> f.txt")
Dir.chdir(wd)
end
I tried both things which are commented, but both of them print the result on the standard exit and in the first one output variable is empty, in the second one the file is created but is empty again :(
Any ideas? Thank you very much in advance! :)
The test framework might have send the result to STDERR. Try use Open3.capture3 to capture the standard error.
require 'open3'
...
stdout, stderr, status = Open3.capture3(%{python "#{File.basename(test)}"})
and write the standard output and standard error to the destination:
File.write("f.txt", stdout + stderr)
You may check status.success? to see if you write the external command right. However, the test framework may return non-zero exit code on failed tests. In that case, you should check the stderr to see the actual error output.
Use Open3.capture2 as below:
output, _ = Open3.capture2("python #{File.basename(test)")
To write output to a file do as below:
File.write("f.txt", output)
I want to call a program multiple times from a python code, and save the output of that program in a text file. My first problem right now is just calling the other code. I have to redirect to a different directory and call ./rank on output.txt. This is how Im trying to do it:
TheCommand = "~/src/rank-8-9-2011/rank output.txt"
os.system(TheCommand)
but im getting a parsing error.
[Parsing error on line ]Unknown error: 0
Im running python2.7 on Mac OS 10.5.8. Im not sure what the problem is. I also tried using subprocess:
subprocess.call(["~/src/rank-8-9-2011/rank", "output.txt"])
This does not find the directory (I have a feeling Im using the subprocess incorrectly), but I dont know what is wrong with the os.system.
the name of the program in the first argument to subprocess.Popen must not contain ~ as it doesn't pass the string to the shell for processing (which like always using parameterized queries in sql, protects one from string injection attacks, e.g. if instead of output.text one had ;rm -rf /, the system version would run rank and then run rm -rf . but the subprocess.Popen would only have rank open a file named ;rm -rf .), so one should expand it by calling os.path.expanduser:
subprocess.Popen([os.path.expanduser('~/src/rank-8-9-2011/rank'), "output.txt"])
although it is possible to turn shell processing on by passing shell=True, it is not recommended for the aforementioned reason.
you should try http://docs.python.org/library/os.path.html#os.path.expanduser
import os.path
subprocess.call([os.path.expanduser("~/src/rank-8-9-2011/rank"), "output.txt"])
I'm fairly certain your parsing error is coming from rank, not from your os.system command, as nothing there looks weird. What happens if you run rank manually?
subprocess seems to have a problem with '~', although I'm not immediately sure why. Put the full path and it should work (although you'll likely get that parsing error if it is indeed a problem with rank).