Python Notes

This will encompass things that I find useful and end up looking up later when I haven't coded in a while.

Python3 Simple HTTP Server

python3 -m http.server <port>

Get password as input

Python 2

import getpass
    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)


Python 3

import getpass

    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)

Run a command from the command line

python -c 'print "A" * 32'

Regex and group matching

>>> import re
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
'Isaac Newton'
('Isaac', 'Newton')

Test your regexs:


List operations

Count elements in a list



String to list



Get first two characters of a string

<string var>[2:]


File operations

Read file line-by-line and print each line

def print_lines(file):
    with open(file) as f:
        for line in f:

Read file into list

def return_list(file):
    with open(file) as f:
        list = f.readlines()
        return list

Remove line from a file containing a specified string

def remove_line_containing_string(file):
    f = open(file, 'r')
    contents = f.readlines()

    f = open (file, 'w')
    for line in contents:
        if not 'some string' in line:

List only files in a directory

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Remove file


Check if file exists

def file_exists(file):
    if os.path.isfile(file):
        return True
        return False

Run command and get output

def run_cmd(cmd):
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    p_status = p.wait()
    return output

List comprehension

This for loop:

for x in range(int(sp),int(ep)+1):

can also be represented as this list comprehension:
[port_scan(site, int(x)) for x in range(int(sp), int(ep)+1)]

Print a line from a list


String interpolation with Python 3.6.x

import os
site = ''
os.system(f"open -a /Applications/Google\ {site}")

String interpolation with older Python

import os
site = ''
os.system("open -a /Applications/Google\ %s" % site)

Multiline list

list = [

Create requirements.txt with pip

pip freeze > requirements.txt

Debugger one-liner

import pdb; pdb.set_trace()

Show global vars in pdb


Show available methods for an object


Show object type


Remove \n and extra quotes from an object

obj.rstrip().replace('"', '')

Check if current user is root, exit if not

def check_root():
    if not os.geteuid() == 0:
        sys.exit('This script must be run as root!')

Dictionary Notes

Add value to list in dictionary if it doesn't exist; otherwise append the value to the existing list.

if not dict.has_key(key):
    dict[key] = [value]

Print a dictionary to a table

Just use and save yourself a headache.

from prettytable import PrettyTable
def output_dict_to_table(in_dict):
        Print an input dictionary as a table

        output_dict_to_table({'Key': ['Value'], 'Key2': ['Value2', 'Value3']})
        t = PrettyTable(['Key', 'Value'])
        for k, v in in_dict.items():
                t.add_row([k, v])
        print t

JSON from output of running a program to dict

This example uses the run_cmd function above.

# Output is bytes; convert to string with the .decode('utf-8')
output = run_cmd('terraform output -json').decode('utf-8')
tf_json = json.loads(output)

The tf_json object will be properly set up as a python dictionary.


Take command line args

Just use argparse; it's clean and can be implemented with minimal fuss.

import argparse

def parse_arg():
    parser = argparse.ArgumentParser(description='Do stuff')
    parser.add_argument("-a", '--argument', required=True, help='description of what your awesome code does')
    return parser.parse_args()
    # Return value of a single argument if you don't have multiple ones:
    #return parser.parse_args().arg


Run remote command using ssh on an aws instance

import paramiko

k = paramiko.RSAKey.from_private_key_file("/path/to/pem/file")
c = paramiko.SSHClient()
c.connect( hostname = "<ec2 hostname>", username = "<username>", pkey = k )

stdin , stdout, stderr = c.exec_command("<command to run>")
print("stdout: " +'utf-8'))
print("stderr" +'utf-8'))


Auto enable virtualenv for a directory

Use, which can be installed via brew, pip, etc.

Once it's installed, add this line to your bash_rc or zshrc or whatever (the path will vary based on which installation method you used):
source /usr/local/opt/autoenv/

Once this is done, you simply drop a .env file into the directory which has a virtualenv, that should look like this:
source .venv/bin/activate

Now whenever you cd into this directory, virtualenv will automatically be activated. You will have to deactivate manually when you leave though.

Commenting Standards

A nice example can be found here for google style.


Set up pipenv for a project with python version 3.6.5

pipenv --python 3.6.5

Install a dependency

pipenv install <package>

Lock the dependencies

You want to do this before a release essentially.
pipenv lock

Run a command from the virtualenv for a project

pipenv run python -V
pipenv run python -c 'print("A" * 32)'

Activate the virtualenv

pipenv shell

Deactivate the virtualenv

CTRL-d or type exit


List licenses of dependencies

pip install pip-licenses