Chapter 3. Working with the Command Line
The command line is where the rubber hits the road. Although there are many powerful tools with graphical interfaces, the command line is still home for DevOps work. Interacting with your shell environment from within Python and creating Python command-line tools are both necessary when using Python for DevOps.
Working with the Shell
Python offers tools for interacting with systems and shells. You should become familiar with the sys
, os
, and subprocess
modules, as all are essential tools.
Talking to the Interpreter with the sys Module
The sys
module offers access to variables and methods closely tied to the Python interpreter.
Note
There are two dominant ways to interpret bytes during reading. The first, little endian, interprets each subsequent byte as having higher significance (representing a larger digit). The other, big endian, assumes the first byte has the greatest significance and moves down from there.
You can use the sys.byteorder
attribute to see the byte order of your current architecture:
In
[
1
]:
import
sys
In
[
2
]:
sys
.
byteorder
Out
[
2
]:
'little'
You can use sys.getsizeof
to see the size of Python objects. This is useful if you are dealing with limited memory:
In
[
3
]:
sys
.
getsizeof
(
1
)
Out
[
3
]:
28
If you want to perform different behaviors, depending on the underlying operating system, you can use sys.platform
to check:
In
[
5
]:
sys
.
platform
Out
[
5
]:
'darwin'
A more common situation is that you want to use a language feature ...
Get Python for DevOps now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.