Python Command Line input? [duplicate] - python

This question already has answers here:
read subprocess stdout line by line
(10 answers)
Closed 21 days ago.
My Python script will run a bunch of shell scripts that output either 200 SOLUTIONS_REVISION or 400 SOLUTIONS_REVISION when run. The 200 in the output indicates success and the 400 indicates failure.
How can I capture these "returned" strings as strs in the Python code, for further processing?

If you're going to run the command from your Python script then you want to look at subprocess with its stdout arguments. If you're going to run both that script and the Python script from a separate shell script then you want to pipe from one to the next and then read from sys.stdin.

Related

How to access both incoming stdout and stderr pipelines from inside python? [duplicate]

This question already has answers here:
Piping both stdout and stderr in bash?
(2 answers)
What does " 2>&1 " mean?
(19 answers)
Closed 3 years ago.
In a bash script, I can access the pipes interchangeably. If I would like to do this from within a python script, how would I go about accessing sys.stderr?
Currently, I am doing the following:
#/usr/bin/env python
import sys
std_input = sys.stdin.readlines()
std_error = sys.stderr.readlines() # <-- fails, complaining that the file is
# not open for reading.
And I call the code from within a bash script:
#/usr/bin/env bash
... | ./python-script.py
Where there exists both stdin and stderr content on the pipeline streaming into the python-script.py command.
There is no need for concurrency or anything fancy.

Suppressing output of a console application when invoked from Python script [duplicate]

This question already has answers here:
Suppress output in Python calls to executables
(9 answers)
Closed 2 years ago.
I'm working on a school project and I have to run John the Ripper through as a Python script.
Currently I'm running the command like this:
subprocess.run(['john --wordlist=/usr/share/john/password.lst --format=sha512crypt /root/Desktop/passwd '], shell=True)
My issue here is that it prints a lot of the process in the command terminal and I can't have anything show up in the terminal.
How can I make it so it doesn't print anything in the terminal?
You want to redirect the print so it dumps it in a file instead of printing on the terminal.
You might want to add something like after your code.
f = open("test.py", "w+")
print(output.stdout,f.write(output.stdout))
If you do cat test.py you should see the output.

How to redirect shell script output ran from a python script [duplicate]

This question already has answers here:
Running shell command and capturing the output
(21 answers)
How do I pipe a subprocess call to a text file?
(2 answers)
Closed 6 years ago.
I am executing few shell scripts from a python script. A particular shell script(last line of the code) call is used to run a Hive query to get some table information and I would like to redirect this output to a file. Here is how my python script looks like.
$ cat test.py
import argparse
from subprocess import call
parser = argparse.ArgumentParser()
parser.add_argument('-c','--cutoff',required=True)
args = parser.parse_args()
Table="elements_status_values_"+ args.cutoff
call(["clear"])
print ">> Running the hive query"
call(["hive", "-S", "-e" "select * from %s order by rand() limit 2;" % cutoffTable])
When I execute this, I get the result on the terminal but I need to redirect the output to a file from python script to do few more operations using the output. So I basically want my Hive query output to be redirected to a file so that I can use the file for other operations within the same script. In shell script we can redirect the output using '>' to a file but is there a way where we can get this done using python?
I have searched posts related to this but all of them are redirecting the python scripts output to a file which I don't want to.
Check out documentation for subprocess.call: you can supply params like stdout and stdin.
with open('output.txt', 'w') as output_file:
call([your_stuff], stdout=output_file)
This is a generic way - similar approach would work in C and in many other languages too.
Old python-specific way would be to use subprocess.check_output instead of subprocess.call.
Current python-specific way would be to use subprocess.run and literally check its stdout: output = run(..., check=True, stdout=PIPE).stdout

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

How to subprocess this call in python: png2pos args > /dev/usb/lp0 [duplicate]

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Closed 7 years ago.
I currently have the following code:
subprocess.call(["png2pos", "-c", "example_2.png", ">", "/dev/usb/lp0"])
The program png2pos is being accessed because it's giving me the message:
This utility produces binary sequence printer commands. Output have to
be redirected
This is the same error I get if I forget to type in > /dev/usb/lp0, so I'm fairly certain it has something to do with the '>' character. How would one redirect this output to /dev/usb/lp0 with subprocess?
To make sure the output is redirected properly, you need to set shell to True and pass a single string:
subprocess.call("png2pos -c example_2.png > /dev/usb/lp0", shell=True)
Otherwise ">" is treated as a program argument.
I do not have your tool installed, so I cannot really test here. But had an issue with redirecting output from a console application using python before. I had to redirect it using the command itself, not via the shell (as you are trying)
with open("/dev/usb/lp0", 'wb') as output_str:
subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)

Categories