Week 07 Tutorial Questions
Objectives
-
In shell we have been using the following hashbang:
#!/bin/dash
How should we modify this hashbang to use it with python?
-
What version of python should be used in this course?
What are the differences between different versions of python?
-
Where can I find the python3(1) documentation?
-
What is a REPL?
How do you start the python REPL?
-
Write a simple version of the
head
command in Python, that accepts an optional command line argument in the form-n
, wheren
is a number, and displays the firstn
lines from its standard input.If the
-n
option is not used, then the program simply displays the first ten lines from its standard input.display first ten lines of file2 ./head.py < file2 same as previous command ./head.py -10 < file2 display first five lines of file2 ./head.py -5 < file2
-
Modify the
head
program from the previous question so that, as well as handling an optional-n
argument to specify how many lines, it also handles multiple files on the command line and displays the firstn
lines from each file, separating them by a line of the form==> FileName <===
.display first ten lines of file1, file2, and file3 ./head.py file1 file2 file3 display first three lines of file1, and file2 ./head.py -3 file1 file2
-
The following is a Python version of the
cat
program.#! /usr/bin/env python3 import sys if len(sys.argv) == 1: sys.argv.append("-") for filename in sys.argv[1:]: try: if filename == "-": stream = sys.stdin else: stream = open(filename) for line in stream: sys.stdout.write(line) if stream != sys.stdin: stream.close() except IOError as e: print(f"{sys.argv[0]}: can not open: {e.filename}: {e.strerror}")
Write a new version of
cat
so that it accepts a-n
command line argument and then prints a line number at the start of each line in a field of width 6, followed by two spaces, followed by the text of the line.The numbers should constantly increase over all of the input files (i.e. don't start renumbering at the start of each file).
./cat.py -n myFile 1 This is the first line of my file 2 This is the second line of my file 3 This is the third line of my file ... 1000 This is the thousandth line of my file
-
Modify the
cat
program from the previous question so that it also accepts a-v
command line option to display all characters in the file in printable form.In particular, end of lines should be shown by a
$
symbol (useful for finding trailing whitespace in lines) and all control characters (ascii code less than 32) should be shown as^X
(whereX
is the printable character obtained by adding the code for 'A' to the control character code). So, for example, tabs (ascii code 9) should display as^I
../cat -v myFile This file contains a tabbed list:$ ^I- point 1$ ^I- point 2$ ^I- point 3$ And this line has trailing spaces $ which would otherwise be invisible.$
-
In Python, you can imitate a main function by using the
if __name__ == '__main__':
construct.How does this work?
Why is this useful?
-
How can we use regular expressions in python?
-
What is the difference between
search
,match
, andfullmatch
? -
How are Python's regular expressions different from grep(1)?