Exercises - Scripting Python for CLI tools#
These exercise should help you either reviewing or learning new shell commands. The exercises can be done localy on Linux, Mac or Windows 11 bash console.
1 Running Python statements from the command line#
We don’t need to open the interactive interpreter to run Python code.
Instead, we can invoke Python with the command flag -c
and the statement we want to run:
%%bash
python -c "print(2+3)"
5
When and why is this useful?
2 Listing files#
A Python library called glob can be used to create a list of files matching a pattern, much like the ls
shell command.
$ python
Python 3.7.6 (default, Jan 8 2020, 13:42:34)
[Clang 4.0.1 (tags/RELEASE_401/final)] ::
Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license"
for more information.
NOTE: run this locally, not possible to enter python shell in a jupyter notebook cell. AND you will like have a newer version of Python installed wich is fine! Python 3.10 or newer is recommended anyways
import glob
glob.glob('zipf/data/*.txt')
['zipf/data/dracula.txt',
'zipf/data/frankenstein.txt',
'zipf/data/jane_eyre.txt',
'zipf/data/moby_dick.txt',
'zipf/data/sense_and_sensibility.txt',
'zipf/data/sherlock_holmes.txt',
'zipf/data/time_machine.txt']
Using script_template.py
as a guide, write a new script called my_ls.py
that takes as input a directory and a suffix (e.g., py, txt, md, sh) and outputs a list of the files (sorted alphabetically) in that directory ending in that suffix.
The help information for the new script should read as follows:
$ python bin/my_ls.py -h
usage: my_ls.py [-h] dir suffix
List the files in a given directory with a given suffix.
positional arguments:
dir Directory
suffix File suffix (e.g. py, sh)
optional arguments:
-h, --help show this help message and exit
and an example of the output would be:
$ python bin/my_ls.py data/ txt
data/dracula.txt
data/frankenstein.txt
data/jane_eyre.txt
data/moby_dick.txt
data/sense_and_sensibility.txt
data/sherlock_holmes.txt
data/time_machine.txt
NOTE: we will not be including this script in subsequent chapters.
3 Sentence ending punctuation#
Our wordcount.py
script strips the punctuation from a text, which means it provides no information on sentence endings.
Using template.py
and wordcount.py
as a guide, write a new script called sentence_endings.py
that counts the occurrence of full stops, question marks and exclamation points and prints that information to the screen.
Hint: String objects have a count
method:
"Hello! Are you ok?".count('!')
1
When you’re done, the script should be able to accept an input file:
$ python bin/sentence_endings.py data/dracula.txt
Number of . is 8505
Number of ? is 492
Number of ! is 752
or standard input:
$ head -n 500 data/dracula.txt | python bin/sentence_endings.py
Number of . is 148
Number of ? is 8
Number of ! is 8
NOTE: we will not be including this script in subsequent chapters.