Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon

Working with Streams in Python

We are all used to working with files on disk. Reading and writing files to disk is second nature to anyone that does more with a computer than just emailing and web surfing. Streams are representation of files in memory. Using standard file i/o, when opening and reading a file on disk, the contents are placed in memory. This "memory file" can be used to write a new file or write changes to the old one. Using the `io` library, a "memory file" can be used to send the contents to other applications without ever writing them to disk. In this lab you will get a chance to use streams to copy a file, and to provide an in-memory file for consumption by a different application. **Please Note:** The contents of `book.txt` was taken from Shakespeare's Sonnets.

Google Cloud Platform icon

Path Info

Clock icon Intermediate
Clock icon 1h 0m
Clock icon Jun 05, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create a Copy for the Editing Department

    Please follow guiding comments in the file. You will be asked to create a function from scratch that takes an input and an output, and then makes a copy of the input to the output.

    import io
    def copy_book(input, output):
        write the input to the output
    if __name__ == "__main__":
        # author's novel is stored in file `book.txt`
        # create a copy of the book for the editing department
        # append _text to the end of the name of the copy
        # student input needed, please use book_input and book_copy
        # as the handles to the files
        book_input = open("book.txt", "r+b")
        book_copy = open("book_edit.txt", "w+b")
        copy_book(book_input, book_copy)
        # test file exists
            f = open("book_edit.txt", "r+b")
        except FileNotFoundError:
            print("book_edit.txt does not exist")
        # test file contents are the same
        assert ==
        # code below this is not shown
  2. Challenge

    Generate a BytesIO Object of Book

    Please follow guiding comments in the file. You will be asked to create a BytesIO object with the book contents. Finally, you will be asked to close all open i/o streams.

        # code above this is not shown
        # write book to a BytesIO object using function copy_book
        # make sure the cursor is at the start of each file (memory file also)
        # send to copy function
        book_stream = io.BytesIO()
        copy_book(book_input, book_stream)
        # test file exists
        if not book_stream.readable():
            print("book_stream does not exist")
        # test
        assert == book_stream.getvalue(), f"Expected: {}\nGot: {book_stream.getvalue()}"
        # close all open files
        # test
        assert book_input.closed == True
        assert book_copy.closed == True
        assert book_stream.closed == True

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans