This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
can we write a script in python that runs dos commands/dos mode?
Eg: I have a command
megarec.exe -cleanflash .1
which supposed to run only on dos mode.I do it manually.Now i need to write a script which automates the above procedure.can i do it?
import subprocess
ch=subprocess.Popen("megarec.exe -cleanflash .1",shell=True,stdout=subprocess.PIPE,stderr=subrocess.PIPE)
output,err=ch.communicate()
if output:
print "success:",output
else:
print "error:",error
Related
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.
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.
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'])
This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Python: How do I redirect this output?
(2 answers)
Closed 8 years ago.
I'm using python subprocess library to run command line in python file.
After importing the library, I used following code to store the output
call(["python", "make.py", ">", "data"])
But for some reason, I didn't get data file
You have to modify the stdout , check the official document subprocess
import subprocess
my_output_file = open("/home/user/output", "a")
subprocess.call(["python", "hello.py"],stdout=my_output_file)
my_output_file.close()
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.