If all you need is the stdout output, then take a look at subprocess.check_output():
import subprocess
batcmd="dir"
result = subprocess.check_output(batcmd, shell=True)
Because you were using os.system(), you'd have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell.
If you need to capture stderr as well, simply add stderr=subprocess.STDOUT to the call:
result = subprocess.check_output([batcmd], stderr=subprocess.STDOUT)
to redirect the error output to the default output stream.
If you know that the output is text, add text=True to decode the returned bytes value with the platform default encoding; use encoding="..." instead if that codec is not correct for the data you receive.
If all you need is the stdout output, then take a look at subprocess.check_output():
import subprocess
batcmd="dir"
result = subprocess.check_output(batcmd, shell=True)
Because you were using os.system(), you'd have to set shell=True to get the same behaviour. You do want to heed the security concerns about passing untrusted arguments to your shell.
If you need to capture stderr as well, simply add stderr=subprocess.STDOUT to the call:
result = subprocess.check_output([batcmd], stderr=subprocess.STDOUT)
to redirect the error output to the default output stream.
If you know that the output is text, add text=True to decode the returned bytes value with the platform default encoding; use encoding="..." instead if that codec is not correct for the data you receive.
import subprocess
string="echo Hello world"
result=subprocess.getoutput(string)
print("result::: ",result)
geoserver rest api - How we can get output for os.system() in python? - Geographic Information Systems Stack Exchange
python - Assign output of os.system to a variable and prevent it from being displayed on the screen - Stack Overflow
how to get the stdout of the script that os.system executes - Post.Byes
python + how to print value that comes from os.system - Unix & Linux Stack Exchange
Videos
I would advice using a library for using REST from python, eg requests http://docs.python-requests.org/en/latest/index.html
r = requests.get('http://localhost:8080/geoserver/rest/workspaces/myworkspace/datastores/mydatastore/featuretypes?list=available', auth=('admin', 'geoserver'))
then use r.text or r.json
it is recommended to use subprocess.call()
cmd must be a list, so you should write it like :
cmdlist= ["curl", "-v", "-u", "admin:geoserver", "-XGET" ,"-H", "Accept: text/xml", "http//jdkjdf"]
note that you can use .split(" ") to create the list based on your variable cmd
From this question which I asked a long time ago, what you may want to use is popen:
os.popen('cat /etc/services').read()
From the docs for Python 3.6,
This is implemented using subprocess.Popen; see that classโs documentation for more powerful ways to manage and communicate with subprocesses.
Here's the corresponding code for subprocess:
import subprocess
proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("program output:", out)
You might also want to look at the subprocess module, which was built to replace the whole family of Python popen-type calls.
import subprocess
output = subprocess.check_output("cat /etc/services", shell=True)
The advantage it has is that there is a ton of flexibility with how you invoke commands, where the standard in/out/error streams are connected, etc.
os.system() just runs the process, it doesn't capture the output:
If command generates any output, it will be sent to the interpreter standard output stream.
The return value is the exit code of the process:
On Unix, the return value is the exit status of the process encoded in the format specified for wait().
You'll need to use something like subprocess.check_output() or subprocess.Popen() directly to capture the output.
>>> arch = subprocess.check_output("uname -a | awk '{print $9}'", shell=True);
>>> arch
'x86_64\n'
You can use subprocess module and achieve this fairly easy.
#!/usr/bin/python3
import subprocess
getVersion = subprocess.Popen("awk '{print $7}' /etc/redhat-release", shell=True, stdout=subprocess.PIPE).stdout
version = getVersion.read()
print("My version is", version.decode())
Also for the awk part, you can use sed 's/[^0-9_.-]//g /etc/redhat-release. This will only extract the version number and doesn't need to know what field version number is which makes it more platform independant.
I am having trouble assigning the output of os.system() as a variable.
When I do the following, it seems to produce the correct output:
>>> var = os.system('date +"%s"')
1409938484However, when I try to print my variable, I get the status code instead:
>>> print var 0
Just entering os.system() into the interpreter gives me the following:
>>> os.system('date +"%s"')
1409938815
0How do I set the variable to the output, not the status code?
Do I really need to use subprocess to call the command and then read stdout to do this? (this seems overly complex for what i am trying to do here)
edit
Here is what I came up with after some searching:
>>> var = os.popen('date +"%s"').readline().strip('\n')
>>> print var
1409940330
>>> It's kinda verbose (and I thought os.popen was deprecated?), but it does what I want it to. I was hoping for something more terse, like you can do in bash e.g. var=$(date +"%s")
edit 2
thanks to u/symmitchry I think this is the best choice at the moment.
var = subprocess.check_output('date +"%s"', shell=True)edit 3
u/Justinsaccount is the winner! just what I wanted in a terse statement.
var = int(time.time())
There are many good SO links on this one. try Running shell command from Python and capturing the output or Assign output of os.system to a variable and prevent it from being displayed on the screen for starters. In short
import subprocess
direct_output = subprocess.check_output('ls', shell=True) #could be anything here.
The shell=True flag should be used with caution:
From the docs: Warning
Invoking the system shell with shell=True can be a security hazard if combined with untrusted input. See the warning under Frequently Used Arguments for details.
See for much more info: http://docs.python.org/2/library/subprocess.html
you can use os.popen().read()
import os
out = os.popen('date').read()
print out
Tue Oct 3 10:48:10 PDT 2017