Read about reading and writing files in Python.
-
Why is it recommended to use the
with
keyword when opening file objects? Why is it important to close a file even if an exception is raised? -
The documentation states that, "Using with is also much shorter than writing equivalent try-finally blocks". What is the
finally
block and, if thewith
keyword did not exist, could you have used it to properly close a file after it was opened? -
The documentation warns:
Calling f.write() without using the with keyword or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully.
Why is this the case? (hint: read about buffered v/s unbuffered I/O).
-
Assuming you have a file object called
f
- what's the difference betweenf.read()
andf.read(n)
(where n is an integer)? -
What does
f.readline()
do? -
The documentation recommends the following method for looping over all the lines in a file:
>>> for line in f: ... print(line, end='')
Why is it important to pass the argument
end=''
to theprint()
function? -
Open a file in binary mode. Print its first byte. Then go to the middle of the file, print its middle byte (or middle two if it has an even numbered size). Then print its last byte.
-
In the list of flags passed to the
open()
function,'a'
opens the file for appending. If this flag did not exist, how can you achieve the same functionality with the'w'
flag?
Read and understand standard streams. This link also explains how to work with standard streams in Python, make sure you read that part too.
- In Python, write a program that behaves like
cat
in its simplest form - each line read from stdin is written to stdout, until the end of input. Test that your program behaves the same way ascat
does when invoked from the shell in the following four ways (read about shell redirection operators if you are not familiar with them).# Read from and write to the terminal python cat.py # Read from the terminal, write to file python cat.py >out.txt # Read from file, write to terminal python cat.py <in.txt # Read and write to file python cat.py <in.txt >out.txt
- Now allow your
cat
equivalent to take one argument - the name of a file to open (e.g.python cat.py in.txt
). If its given, your program should open and read from this file instead of the standard input. If the file does not exist, or cannot be opened for some other reason, print an error message on stderr and exit. - When your cat equivalent is simply reading from the terminal (i.e. stdin is not redirected), can you send it an EOF from the terminal?
- Now write a
tee
equivalent. Thetee
utility optinally takes one or more filenames as arguments. It copies stdin to its stdout, likecat
. In addition, it also writes the contents of stdin to each file passed to it as an argument. e.g.tee f1.txt f2.txt
will read from its stdin and copy its contents to its stdout and also the files f1.txt and f2.txt. - Can you call the
seek()
method on sys.stdin? - When you call the
print()
function, on which stream does it write its output? Can you tell print to write something to stderr instead? - What's the difference between
sys.stdout.write()
andprint()
. When would you prefer calling one over the other?