CLI Arguments, File I/O, Modules

BCH 519
Spring 2021

Andrew E. Bruno

Topics Covered

  • Command line arguments
  • File I/O
  • Modules, PyPI
  • Debugging

Command Line Arguments

Passing arguments to python script

  • Your python script is run from the command line
  • Recall the basic form of a unix command:
    • command options arguments
  • You can pass “arguments” to your python script just like any other unix command
  • Allows you to pass input or “data” into your programs
  • Example:
python3 arg1 arg2 arg3 ..

The special list: sys.argv

  • Python sys module has a number of special pre-defined variables
  • sys.argv is a special list containing the command line arguments to your script
  • For the more info:

Exercise 1: echo in python

# sys.argv is a list of command line arguments passed to 
# your script. Here, we simply print or "echo" the first 
# argument that was passed to our script.
# Note: sys.argv[0] is the name of the script. 
# The first argument starts at: sys.argv[1]
import sys


run in the terminal:

$ python3 Hello
$ python3 Hello World
$ python3 "Hello World"
Hello World

Exercise 1: What we learned

  • sys.argv is a list of command line arguments passed to your script
  • sys.argv[1] The arguments start at index 1. Index 0 is the name of the script
  • What happens when you run with no arguments? How can you fix this?

Exercise 2: add two numbers

import sys

# Good to always validate any input to your program
if len(sys.argv) != 3:
    print("Usage: python [num1] [num2]")

num1 = sys.argv[1]
num2 = sys.argv[2]
total = int(num1) + int(num2)

print(f"Sum: {total}")

run in the terminal:

$ python3 2 15
Sum: 17

Exercise 2: What we learned

  • Good practice to always validate any input to your programs
  • sys.exit function exits program immediately
  • What happens when you run with the following input? How can you fix this?
$ python3 bob bill

File I/O


  • Data is typically stored in files
  • We can write programs to process and mainpulate data stored in files
  • Three basic file operations: read, write, append

File Objects

  • Files objects contain functions for working with files
  • Special file objects: sys.stdout and sys.stdin
  • More details:

sys.stdout = Standard Output

  • We’ve actually been working with this file object all along
  • Standard output refers to the output of a program (what get’s printed to the screen)
  • Python has a file object called sys.stdout
  • print statement “writes” to a file object, by default sys.stdout
import sys

print("Hello World")

# Is the same as 

print(sys.stdout, "Hello World")

Opening and closes files

  • File objects are created in a “mode” using open function:
    • 'r' = read
    • 'w' = write
    • 'a' = append
  • When your done, close your file using file.close:
# Open file for reading
fin = open('input.txt', 'r')

# Open file for writing
fout = open('input.txt', 'w')


Writing to Files

  • Open a file object in write mode: 'w'
  • Use the file.write function to write to the file object
fout = open('output.txt', 'w')
fout.write("Hello World\n")

Reading Files

  • Open a file object in read mode: 'r'
  • Good practice to use with keyword. Will properly close files for you
  • File objects in python are iterators

Reading Files Example Code

with open('input.txt', 'r') as fin:
    # Read entire file
    contents =

    # Read all lines
    lines = fin.readlines()

    # Read single line
    line = fin.readline()

    # Loop through lines of file
    for line in fin:
        # Force print to suppress newline end=''
        print(line, end='') = Standard Input

  • Standard input refers to the data going into a program (data provided as input)

  • Python uses a file object called sys.stdin

    import sys
    # Read line from STDIN
    line = sys.stdin.readline()
    print(line, end='')
  • Example program execution, ‘|’ pipe will send the output of the echo command as input into our python script:

    $ echo "Hello World" | python3
    Hello World

Typical I/O Scenario

  • Get command line arguments
  • Open files for reading and/or writing
  • Read data and process
  • Write output
  • Close files

Exercise 3: cat in python

import sys

if len(sys.argv) != 2:
    print("Usage: python [filename]")

filename = sys.argv[1]
with open(filename, 'r') as fin:
    for line in fin:
        print(line, end='')

run in the terminal:

$ echo "Hello World" > test-file.txt
$ cat test-file.txt
Hello World
$ python3 test-file.txt
Hello World

Exercise 4: grep in python

import sys

if len(sys.argv) != 3:
    print("Usage: python [pattern] [filename]")

pattern = sys.argv[1]
filename = sys.argv[2]
with open(filename, 'r') as fin:
    for line in fin:
        if pattern in line:

Exercise 4: grep in python output

Run in the terminal:

# Test using snps.bed from HW1
$ python3 Glioma snps.bed | wc -l
$ grep Glioma snps.bed | wc -l

Exercise 3 & 4: What we learned

  • sys.argv contains the command line arguments
  • with closes the open file object automatically
  • print(line, end='') will ommit printing a newline
  • string.strip() will remove leading and trailing whitespace
  • We can re-write linux commands in Python!!


What is a Module?

  • A set of related functions in a “library” file
  • Designed to be reusable by other modules or programs
  • Code reuse! Repeating same code is bad

Simple Python module example


greeting = 'Hello World'
salutation = 'Goodbye'

def power(num, pow):
    return num ** pow

How to use Python modules?


import mathfunc

sq = mathfunc.power(8, 2)
print(f"8 squared = {sq}")


  • The Python Package Index
  • A.K.A “the cheese shop”
  • Contains over 40K Python modules

Example: Biopython

  • Set of freely available tools for biological computation written in Python by an international team of developers
  • Collection of many Python modules and related documenation

Parsing FASTA files

  • Sequencing data is often stored in a simple text based format called FASTA
  • Begins with a single line description, followed by lines of sequencing data
  • Instead of re-inventing the wheel and writing our own FASTA parser. Let’s use a module from Biopython

Exercise 5: Parsing FASTA files with Biopython

from Bio import SeqIO

for rec in SeqIO.parse("mirna-targets.fasta", "fasta"):
    print(f"{len(rec)} {rec.seq}")

Run in the terminal (on vortex):

$ module load bioconda/py37-biopython
$ python3

Debugging - pprint

  • Excellent python module for debugging your code
  • “Data pretty printer”
  • Allows you to “dump” the contents of a variable
  • Example: dump the value of an entire hash

Exercise 6: pprint

import pprint

data = [
        'id': 124,
        'name': 'miR-245',
        'chrom': 'chr10'
        'id': 234,
        'name': 'miR-201',
        'chrom': 'chr11'


Homework #3

Due: 2021-03-02 09:00:00