Shell Commands used in Python - MyPythonGuru

Jobs Search Portal and Learning point for Python,Data Science,AI,ML, Cloud and latest technologies.

Follow us on Facebook

Post Top Ad

Your Ad Spot

Tuesday, July 23, 2019

Shell Commands used in Python

Quick Introduction to the Shell

Below is sample of a Linux/OSX shell session where a user explores, creates, and modifies directories and files on their system (osx:~ $ is the prompt, and everything after the $ sign is the typed command; text that is preceded by a # is meant just as comment, rather than something you would actually type in):
osx:~ $ echo "hello world"             # echo is like Python's print function
hello world

osx:~ $ pwd                            # pwd = print working directory
/home/mpg                            # this is the "path" that we're sitting in

osx:~ $ ls                             # ls = list working directory contents
notebooks  projects

osx:~ $ cd projects/                   # cd = change directory

osx:projects $ pwd
/home/mpg/projects

osx:projects $ ls
datasci_book   mpld3   myproject.txt

osx:projects $ mkdir myproject          # mkdir = make new directory

osx:projects $ cd myproject/

osx:myproject $ mv ../myproject.txt ./  # mv = move file. Here we're moving the
                                        # file myproject.txt from one directory
                                        # up (../) to the current directory (./)
osx:myproject $ ls
myproject.txt
Notice that all of this is just a compact way to do familiar operations (navigating a directory structure, creating a directory, moving a file, etc.) by typing commands rather than clicking icons and menus. Note that with just a few commands (pwdlscdmkdir, and cp) you can do many of the most common file operations. It's when you go beyond these basics that the shell approach becomes really powerful.
Shell Commands in IPython
Any command that works at the command-line can be used in IPython by prefixing it with the ! character. For example, the lspwd, and echocommands can be run as follows:
In [1]: !ls
myproject.txt

In [2]: !pwd
/home/jake/projects/myproject

In [3]: !echo "printing from the shell"
printing from the shell
Passing Values to and from the Shell
Shell commands can not only be called from IPython, but can also be made to interact with the IPython namespace. For example, you can save the output of any shell command to a Python list using the assignment operator:
In [4]: contents = !ls

In [5]: print(contents)
['myproject.txt']

In [6]: directory = !pwd

In [7]: print(directory)
['/Users/jakevdp/notebooks/tmp/myproject']
Note that these results are not returned as lists, but as a special shell return type defined in IPython:
In [8]: type(directory)
IPython.utils.text.SList
This looks and acts a lot like a Python list, but has additional functionality, such as the grep and fields methods and the sn, and p properties that allow you to search, filter, and display the results in convenient ways. For more information on these, you can use IPython's built-in help features.
Communication in the other direction–passing Python variables into the shell–is possible using the {varname} syntax:
In [9]: message = "hello from Python"

In [10]: !echo {message}
hello from Python
The curly braces contain the variable name, which is replaced by the variable's contents in the shell command.
Shell-Related Magic Commands
If you play with IPython's shell commands for a while, you might notice that you cannot use !cd to navigate the filesystem:
In [11]: !pwd
/home/mpg/projects/myproject

In [12]: !cd ..

In [13]: !pwd
/home/mpg/projects/myproject
The reason is that shell commands in the notebook are executed in a temporary subshell. If you'd like to change the working directory in a more enduring way, you can use the %cd magic command:
In [14]: %cd ..
/home/mpg/projects
In fact, by default you can even use this without the % sign:
In [15]: cd myproject
/home/mpg/projects/myproject
This is known as an automagic function, and this behavior can be toggled with the %automagic magic function.
Besides %cd, other available shell-like magic functions are %cat%cp%env%ls%man%mkdir%more%mv%pwd%rm, and %rmdir, any of which can be used without the % sign if automagic is on. This makes it so that you can almost treat the IPython prompt as if it's a normal shell:
In [16]: mkdir tmp

In [17]: ls
myproject.txt  tmp/

In [18]: cp myproject.txt tmp/

In [19]: ls tmp
myproject.txt

In [20]: rm -r tmp
This access to the shell from within the same terminal window as your Python session means that there is a lot less switching back and forth between interpreter and shell as you write your Python code.

Reference :Stackoverflow , python-course

No comments:

Post Top Ad

Your Ad Spot