os.system not writing in terminal [duplicate] - python

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'])

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 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)

How to run dos commands in python? [duplicate]

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

Python Command Line input? [duplicate]

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.

Categories