Execute shell command and use it's output in Jupyter - python

I know this is a recurrent question, but I can't find a useful answer.
In Python, for running a shell command one can use this.
If I do the same inside Jupyter I got no output. How can I see the results
of executing the command? Doing
print subprocess.call(["ping", "-c 2", "www.cyberciti.biz"])
returns zero.

Use the ! shell magic:
!ping -c 2 www.cyberciti.biz
If you want to assign it to a variable:
output = !ping -c 2 www.cyberciti.biz
print(output)

Related

linux command pipe with python "-c" flag

I am trying to do a string printing with python -c flag, e.g.
python3 -c "print('Hello World')"
So now I wanna substitute an argument with pipe, e.g. echo "Hello World" | python3 -c "print($1)"
the pipe is to take output from previous command and take it as input to next command, if I am not wrong, this is possible? But I think I got syntax error which I cannot find any source of this
I also bumped into question previously asked, but the solution required python imports and .py file depends on how we run this, I understand but I just wanna get it in a line of command in linux shell
If your input is always single line then you should be able to harness input function for example
echo "Hello World" | python3 -c "print(input().upper())"
would output
HELLO WORLD

Printing .py file output in command line

I am trying to access a python function from the command line, and I would like to write such a command that will print the output in the terminal. The below doesn't work. What could I change?
python -c 'from laser import Laser; laser = Laser();l = laser.embed_sentences("hello", lang = "en").shape == (1, 1024); print(l)'
(base) ~ % python -c 'print("hello, world")'
hello, world
Printing works fine for me when running python through python -c. Are you sure your terminal isn't truncating your output by omitting the last (and in this case, only) line? You could try creating a single line file (no newline at the end) and then running cat [filename] (which is how I sometimes discover that my terminal is doing this)
-c cmd : program passed in as string (terminates option list)
That is the correct flag to be used. This must be a CLI config issue. Or the script is taking longer than you are expecting to run and it appears no output is generated.
Does python -c 'print("hello")' work?

How to execute a command in the terminal from a Python script?

I want to execute a command in terminal from a Python script.
./driver.exe bondville.dat
This command is getting printed in the terminal, but it is failing to execute.
Here are my steps:
echo = "echo"
command="./driver.exe"+" "+"bondville.dat"
os.system(echo + " " + command)
It should execute the command, but it's just printing it on terminal. When feeding the same thing manually it's executing. How do I do this from a script?
The echo terminal command echoes its arguments, so printing the command to the terminal is the expected result.
Are you typing echo driver.exe bondville.dat and is it running your driver.exe program?
If not, then you need to get rid of the echo in the last line of your code:
os.system(command)
You can use the subprocess.check_call module to run the command, you don't need to echo to run the command:
from subprocess import check_call
check_call(["./driver.exe", "bondville.dat"])
Which is equivalent to running ./driver.exe bondville.dat from bash.
If you wanted to get the output you would use check_outout:
from subprocess import check_output
out = check_output(["./driver.exe", "bondville.dat"])
In your own code you are basically echoing the string command not actually running the command i.e echo "./driver.exe bondville.dat" which would output ./driver.exe bondville.dat in your shell.
Try this:
import subprocess
subprocess.call("./driver.exe bondville.dat")

ps -o cmd=<PID> Not Give Same Output From Python

From Python:
output = os.popen("ps -o cmd=1").read()
print output
Output:
1
/bin/bash
python myPython.pyc
sh -c ps -o cmd=1
ps -o cmd=1
But when I run that command from terminal it returns what I want:
/sbin/init
Also, when I run "ls -l" command from python, it returns correct thing.
My main purpose is finding name of process from its PID in Python.
What should I do ?
This does not answer the question regarding why you get different output, but a better approach to solving the goal you are after is to either:
Open and read /proc/<pid>/cmdline, or
Read the symbolic link /proc/<pid>/exe
EDIT: Get rid of the popen call there and the subsequent "useless use of cat". Do this instead:
with open("/proc/"+data.get("pid")+"/cmdline") as cmd:
cmdinfo=cmd.read()
command=cmdinfo.split("\0")
print command[0]
I know you have an answer now, but the reason that your original attempt did not work is probably because popen creates a brand new process, and therefore a different process environment.
When I run 'ps -o cmd=1' from my Terminal, I get similar results as you did when you used popen.
1
bash
ps -o cmd=1

Have python generate command line parameter

I'm trying to have python generate the input parameter to my command line program (Linux), and simply cannot get it to work.
I know it is something to the effect of
./heap0 (python -c 'print "A"*72)
but that does not work....
Try $(). It takes the output of a command and includes it as a value.
./heap0 $(python -c 'print "A"*72')

Categories