%%file three.txt
One
Two
ThreeOverwriting three.txt
October 9, 2023
Write a program cat.py that takes one or more filenames are command-line arguments and prints the contents of them.
$ python cat.py files/five.txt
one
two
three
four
five
$ python cat.py files/abcd.txt files/1234.txt
A
B
C
D
1
2
3
4
You can verify your solution using:
%verify_problem cat
Just like strings, python has a bytes type.
To read a binary file, we need to open it in rb (read-binary) mode.
To write a file, we need to open the file in write mode.
Whenever, we open a file in write mode, all the the contents of that file will be wiped out.
Also, it is import to close a file after writing. Only after a file closed, the contents will be synced to the disk.
Q: How to find doucumentation?
f.write<shift+tab> -- shows help in a popup
f.write? -- adds help to the notebook
Try help on any other function.
len<shift+tab>
len?
with statementThe with statement is handy to close a file automatically at the end of the code block.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Whenever we open a text file, we open that with an encoding.
When we open a file, python read the bytes from the file, decodes them using an encoding and gives back the text.
The default encoding is usually utf-8.
How to read binary files then?
Similary when we write, we use mode wb.
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Write a program copy_file.py to copy contents of one file to another.
The program should accept a source file and a destination file as arguments and copy the source to the destination.
$ python copyfile.py files/five.txt 5.txt
Note: Don't call this file copy.py as that interfere with a standard library module with the same name.
You can verify your solution using:
%verify_problem copy-file
Usage: wc [OPTION]... [FILE]...
or: wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. A word is a non-zero-length sequence of
printable characters delimited by white space.
With no FILE, or when FILE is -, read standard input.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
-c, --bytes print the byte counts
-m, --chars print the character counts
-l, --lines print the newline counts
--files0-from=F read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-L, --max-line-length print the maximum display width
-w, --words print the word counts
--help display this help and exit
--version output version information and exit
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'
one two three
one two three four
one two three four five
two three four five
three four five
one-two-three-four-five-six-seven
Traceback (most recent call last):
File "/home/jupyter-anand/book/square.py", line 4, in <module>
n = int(sys.argv[1])
ValueError: invalid literal for int() with base 10: '--help'
We’ll build a simple command-line app that just says hello to a name.
%%file hello.py
import argparse
p = argparse.ArgumentParser()
p.add_argument("name", help="name to say hello")
args = p.parse_args()
print(args)
print("Hello", args.name)Overwriting hello.py
usage: hello.py [-h] name
hello.py: error: the following arguments are required: name
usage: hello.py [-h] name
positional arguments:
name name to say hello
options:
-h, --help show this help message and exit
Let’s improve that by adding flags.
%%file hello2.py
import argparse
p = argparse.ArgumentParser()
p.add_argument("name", help="name to say hello")
p.add_argument("-r", "--repeats", type=int, default=1, help="number of times to repeat the message")
args = p.parse_args()
print(args)
for i in range(args.repeats):
print("Hello", args.name)Writing hello2.py
Namespace(name='Python', repeats=4)
Hello Python
Hello Python
Hello Python
Hello Python
Namespace(name='Python', repeats=4)
Hello Python
Hello Python
Hello Python
Hello Python
%%file hello3.py
import argparse
p = argparse.ArgumentParser()
p.add_argument("name", help="name to say hello")
p.add_argument("-r", "--repeats", type=int, default=1, help="number of times to repeat the message")
p.add_argument("-u", "--uppercase",
action="store_true",
default=False,
help="convert the message into uppercase")
args = p.parse_args()
# print(args)
msg = f"Hello {args.name}"
if args.uppercase:
msg = msg.upper()
for i in range(args.repeats):
print(msg)Overwriting hello3.py
usage: hello3.py [-h] [-r REPEATS] [-u] name
positional arguments:
name name to say hello
options:
-h, --help show this help message and exit
-r REPEATS, --repeats REPEATS
number of times to repeat the message
-u, --uppercase convert the message into uppercase
Problem: Rewrite square.py that we wrote earlier by using argparse.
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.
Pattern selection and interpretation:
-E, --extended-regexp PATTERNS are extended regular expressions
-F, --fixed-strings PATTERNS are strings
-G, --basic-regexp PATTERNS are basic regular expressions
-P, --perl-regexp PATTERNS are Perl regular expressions
-e, --regexp=PATTERNS use PATTERNS for matching
-f, --file=FILE take PATTERNS from FILE
-i, --ignore-case ignore case distinctions in patterns and data
--no-ignore-case do not ignore case distinctions (default)
-w, --word-regexp match only whole words
-x, --line-regexp match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
Output control:
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print file name with output lines
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only nonempty parts of lines that match
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=GLOB search only files that match GLOB (a file pattern)
--exclude=GLOB skip files that match GLOB
--exclude-from=FILE skip files that match any file pattern from FILE
--exclude-dir=GLOB skip directories that match GLOB
-L, --files-without-match print only names of FILEs with no selected lines
-l, --files-with-matches print only names of FILEs with selected lines
-c, --count print only a count of selected lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--group-separator=SEP print SEP on line between matches with context
--no-group-separator do not print separator for matches with context
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
When FILE is '-', read standard input. With no FILE, read '.' if
recursive, '-' otherwise. With fewer than two FILEs, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: bug-grep@gnu.org
GNU grep home page: <https://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
one two three
one two three four
one two three four five
two three four five