Skip to content

Instantly share code, notes, and snippets.

@chaitanyagupta
Last active March 15, 2021 10:20
Show Gist options
  • Save chaitanyagupta/bb10e1040ddac3617bb8952be1272939 to your computer and use it in GitHub Desktop.
Save chaitanyagupta/bb10e1040ddac3617bb8952be1272939 to your computer and use it in GitHub Desktop.
Learning about files in Python

Files in Python

Basics

Read about reading and writing files in Python.

  1. 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?

  2. The documentation states that, "Using with is also much shorter than writing equivalent try-finally blocks". What is the finally block and, if the with keyword did not exist, could you have used it to properly close a file after it was opened?

  3. 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).

  4. Assuming you have a file object called f - what's the difference between f.read() and f.read(n) (where n is an integer)?

  5. What does f.readline() do?

  6. 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 the print() function?

  7. 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.

  8. 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?

Standard Streams

Read and understand standard streams. This link also explains how to work with standard streams in Python, make sure you read that part too.

  1. 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 as cat 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
    
  2. 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.
  3. 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?
  4. Now write a tee equivalent. The tee utility optinally takes one or more filenames as arguments. It copies stdin to its stdout, like cat. 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.
  5. Can you call the seek() method on sys.stdin?
  6. When you call the print() function, on which stream does it write its output? Can you tell print to write something to stderr instead?
  7. What's the difference between sys.stdout.write() and print(). When would you prefer calling one over the other?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment