Python -functions/modules/system calls

opening a file
Exit codes
using functions
import system Linux os calls
create your own module
In build python hello world
Import modules
Check modules
Install new software packages


if you have a list or a word in memory and you want to use the second item see the following, you can also use [-1] # last character or [-2] # second-last character

cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
print cats[2]
# ./list

opening a file

openfile = open('pathtofile', 'r')

Exit codes(

exit(0) means a clean exit without any errors / problems

exit(1) means there was some issue / error / problem and that is why the program is exiting.

Using functions

To save yourself repeating code (and meaning you only need to change the code in 1 place if its called many times)

# cat func
def hello():
print "hello and welcome"


# ./func
hello and welcome
hello and welcome
hello and welcome

import system Linux os calls

to import the native linux commands:

cat os.system
import os
[root@testautoinstall4 python]# ./os.system
Tue Sep 23 04:48:32 EDT 2014

to check where the os is imported from run
>>> import os
>>> print os.__file__
to change the sys path add the code:


that means the python path is imported from here
any following imports i.e

import fstab

will import the from that location

create your own module

creating modules rules:

give it a py extension
make sure you don't call it a number
the module doesn't need to be excutable

[root@test-scripting-OCt2015 11wed]# cat
def x():
print "hello and welcome"


[root@test-scripting-OCt2015 11wed]# cat ./rem3
import test2

[root@test-scripting-OCt2015 11wed]# ./rem3
hello and welcome
hello and welcome

good page on modules

In build python hello world

>>> import __hello__
Hello world...

Import modules

Modules allow you to use some of the inbuilt system commands to do more with code

The OS module in Python provides a way of using operating system dependent
functionality, example of usage

import os
f = os.popen('date')
now =
print "Today is ", now

Today is Tue Nov 11 11:32:11 EST 2014

More on python modules from

Locating Modules

When you import a module, the Python interpreter searches for the module in the following sequences −

The current directory.

If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.

If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

Check modules

to get list of attributes for a module

>>> import terminal
SyntaxError: invalid syntax
>>> dir (terminal)
['Color', 'Command', 'Logger', 'Option', '__author__', '__builtins__', '__doc__', '__file__', '__homepage__', '__name__', '__package__', '__path__', '__version__', 'absolute_import', 'black', 'black_bg', 'blink', 'blue', 'blue_bg', 'bold', 'choose', 'color', 'colorize', 'command', 'conceal', 'confirm', 'cyan', 'cyan_bg', 'faint', 'getpass', 'gray', 'gray_bg', 'green', 'green_bg', 'grey', 'grey_bg', 'hex2ansi', 'inverse', 'is_256color_supported', 'is_color_supported', 'italic', 'log', 'magenta', 'magenta_bg', 'os', 'overline', 'password', 'prompt', 'red', 'red_bg', 'rgb2ansi', 'strike', 'string_type', 'sys', 'underline', 'white', 'white_bg', 'yellow', 'yellow_bg']

or nicer output
for i in dir(terminal): print i


Install new software packages

pip is a package management system used to install and manage software packages written in Python

yum -y install python-pip
pip install pudb
pip help
pip list

pip freeze
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

more reading at

to install an old version of pip (when you have more than 2 versions of python running)
# whereis pip
pip: /usr/bin/pip3.4 /usr/bin/pip /usr/bin/pip2.6
pip-2.7 install mybarpackage