Learning Python

Submitted by olaf on 2014-12-02
Last modified at 2016-05-06

When it comes to scripting, I usually start with a simple shell script. If it gets a bit more complicated, I then move to awk or Perl. But it never hurts to learn another language, if only to gather new insights.

Here’s a simple cheat sheet, a list of Python idioms I found somewhere on the web. Resources include

Looping over command line file input

import fileinput
for line in fileinput.input():

Reading a file at once with file.read

f = open('example.txt')
content = f.read()

Reading a file into a list with file.readlines

f = open('example.txt')
lines = f.readlines()


lines = [line.strip() for line in open('example.txt')]

Open a file in user’s home directory, denoted by a tilde ~

filepath = os.path.expanduser('~/example.txt')
f = open(filepath)

Convert bytes to a string


Matching a regular expression

import re
m = re.search('some(.*)regular\s*expression', source, re.IGNORECASE)
if m:
    s = m.group(1)

Ternary operator (FAQ)

stmt1 if condition else stmt2

Download a web page

import urllib2
page = urllib2.urlopen('http://www.example.com')
# do something with HTML, e.g.
for line in page:

Same with Python 3’s urllib.request

import urllib.request
response = urllib.request.urlopen('http://www.example.com')
for line in response:

Fixing error UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 6: ordinal not in range(128).


print s


print s.encode('utf-8')

or whatever encoding you use.

Accessing command line arguments

import sys

Starting a subprocess

import subprocess
retcode = subprocess.call(['ls', '-l'])

Create a pipeline with subprocess. Feed standard output of first command to standard input of the second command. Same as a shell pipeline find -name '*.py' | xargs grep import

import subprocess
p1 = subprocess.Popen(['find', '-name', '*.py'], stdout = subprocess.PIPE)
retcode = subprocess.call(['xargs', 'grep', 'import'], stdin = p1.stdout)

Parsing an HTML document with html.parser

import urllib.request
from html.parser import HTMLParser

class PrintAnchorAttributes(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            for attr in attrs:

response = urllib.request.urlopen('http://www.example.com')
parser = PrintAnchorAttributes()
for line in response:

Simple debug logging to a file

import logging
logging.basicConfig(filename = 'debug.log', level = logging.DEBUG)

Common string operations and String Methods

Print without newline

print('Hello, world!', end = '')

and in Python 2

sys.stdout.write('Hello, world!')

or with appended space

print('Hello, world!'),

Boolean values are capitalized! True and False

Reading input from user: input([prompt])

Post a comment

All comments are held for moderation; Markdown and basic HTML formatting accepted. If you want to stay anonymous, leave name, e-mail and website empty.