Why does !! in Python run commands but encase the output in brackets

Question:

A typeo produced an unexpected result and just in case it has a use, I want to understand it.

!!C:/myPythonPath/python script/event.py

Single ! runs a command (DOS, LINUX, other commands you could normally execute from a command line) from within Python, in this case, from with a Jupyter Notebook cell of Python 2.7.

I mistakenly typed two exclamation points and surprisingly, the code still worked, but my output appeared inside of square brackets as in:

[ output of my program ]

What’s more, the presentation looks like it put the output in a list. Here is an example:

['------------------------------',
 'Unknown Event: Django Girls Bucaramanga, Colombia',
 '    Time    :  08 April – 09 April  2017',
 '    Location: Bucaramanga, Colombia',
 '------------------------------',
 'Upcoming Event: Python ... ]

If this is truly a list, is there a way to capture it for later use in a program that is calling a script? I tried adding in an x = into the original line, but this did not work for me. Maybe I did not set up the line right. Is there a way to do this? This is just a point of curiosity.

Asked By: TMWP

||

Answers:

Comments provided by @user2357112 held the answer. Admittedly, you have to find it in a sea of other useful topics, and the actual syntax to solve the problem is not clearly stated in the write-ups.

The answer: Yes, !! does convert output of ! into a list. But attempts to capture it from !!, as the links below indicate is possible, do not seem to work.

Again from @user2357112, this syntax (which does not use the !!) will achieve the desired effect (run the command and capture it in a list):

x = %sx echo "some text"

Print x and you will get:
["some text"]

Links for more reading so they don’t get buried in the comments:

  1. https://www.datacamp.com/community/blog/ipython-jupyter#gs.ImXit1s
  2. https://ipython.org/ipython-doc/3/interactive/reference.html#manual-capture-of-command-output-and-magic-output
Answered By: TMWP

All the details here -> https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-sx

copy/paste:

     %sx¶
    
        Shell execute - run shell command and capture output (!! is short-hand).
    
        %sx command
    
        IPython will run the given command using commands.getoutput(), and

 return the result formatted as a list (split on ‘n’). Since the output is
 
_returned_, it will be stored in ipython’s regular output cache Out[N] and in 

the ‘_N’ automatic variables.

I performed some tests (full code below so you can check yourself)

str_x="some text x"
str_y="some text y"
str_z="some text z"

%sx echo {str_x} ;echo -e {str_x}
_
x = %sx echo {str_x}; echo {str_x}
x
!echo {str_y}; echo {str_y}
y = !echo {str_y}; echo {str_y}
y
!!echo {str_z}; echo {str_z}
_
z = !!echo {str_z}; echo {str_z}
_
z

Individual outputs (python 3.7)

enter image description here

Some conclusions:

  • !! is a shortcut for %sx
  • !! cannot be assigned to variable, but the ouput is on _
  • ! and %sx can be assigned to a variable, in any case ouput is on _
  • ! output is natural, while %sx output is a list of the output (the output converted to a list)

If you look at the first link, %sx is a special case for %sc.

%sx differs from %sc in that %sx automatically splits into a list,
like ‘%sc -l’. The reason for this is to make it as easy as possible
to process line-oriented shell output via further python commands. %sc
is meant to provide much finer control, but requires more typing.

Note: Don’t be fooled by the last _. The command before it failed, so what it stores is the output of the previous one.

!!echo {str_z}; echo {str_z}
_
z = !!echo {str_z}; echo {str_z}
_  # This one is not the output of the preceding line
z
Answered By: Rub
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.