python3 myscript.py arg1 arg2 arg3 ..
sys
module has a number of special pre-defined variablessys.argv
is a special list containing the command line arguments to your script# 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
print(sys.argv[1])
$ python3 ex1.pl Hello
Hello
$ python3 ex1.pl Hello World
Hello
$ python3 ex1.pl "Hello World"
Hello World
sys.argv
is a list of command line arguments passed to your scriptsys.argv[1]
The arguments start at index 1. Index 0 is the name of the scriptimport sys
# Good to always validate any input to your program
if len(sys.argv) != 3:
print("Usage: python ex16.py [num1] [num2]")
sys.exit(1)
num1 = sys.argv[1]
num2 = sys.argv[2]
total = int(num1) + int(num2)
print(f"Sum: {total}")
$ python3 ex2.py 2 15
Sum: 17
sys.exit
function exits program immediately$ python3 ex2.py bob bill
sys.stdout
and sys.stdin
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")
open
function:
'r' = read
'w' = write
'a' = append
file.close
:# Open file for reading
fin = open('input.txt', 'r')
# Open file for writing
fout = open('input.txt', 'w')
fin.close()
fout.close()
'w'
file.write
function to write to the file objectfout = open('output.txt', 'w')
fout.write("Hello World\n")
fout.close()
'r'
with
keyword. Will properly close files for youwith open('input.txt', 'r') as fin:
# Read entire file
contents = fin.read()
# 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 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 test.py
Hello World
import sys
if len(sys.argv) != 2:
print("Usage: python ex3.py [filename]")
sys.exit(1)
filename = sys.argv[1]
with open(filename, 'r') as fin:
for line in fin:
print(line, end='')
$ echo "Hello World" > test-file.txt
$ cat test-file.txt
Hello World
$ python3 ex3.py test-file.txt
Hello World
import sys
if len(sys.argv) != 3:
print("Usage: python ex4.py [pattern] [filename]")
sys.exit(1)
pattern = sys.argv[1]
filename = sys.argv[2]
with open(filename, 'r') as fin:
for line in fin:
if pattern in line:
print(line.strip())
# Test using snps.bed from HW1
$ python3 ex4.py Glioma snps.bed | wc -l
28
$ grep Glioma snps.bed | wc -l
28
sys.argv
contains the command line argumentswith
closes the open file object automaticallyprint(line, end='')
will ommit printing a newlinestring.strip()
will remove leading and trailing whitespacegreeting = 'Hello World'
salutation = 'Goodbye'
def power(num, pow):
return num ** pow
import mathfunc
sq = mathfunc.power(8, 2)
print(mathfunc.greeting)
print(f"8 squared = {sq}")
print(mathfunc.salutation)
from Bio import SeqIO
for rec in SeqIO.parse("mirna-targets.fasta", "fasta"):
print(rec.id)
print(f"{len(rec)} {rec.seq}")
$ module load bioconda/py37-biopython
$ python3 ex5.py
hg19_targetScanS_SAMD11:miR-504
8 CAGGGTCA
...
import pprint
data = [
{
'id': 124,
'name': 'miR-245',
'chrom': 'chr10'
},
{
'id': 234,
'name': 'miR-201',
'chrom': 'chr11'
},
]
pprint.pprint(data)