We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Read File

Let's start ackshually building "bookbot"! Here are some things you'll need to know how to do for this step:

with Block

A with block can be used to open a file:

with open(path_to_file) as f:
    # do something with f (the file) here

The with block will automatically close the file when the block is exited, cleaning up resources.

.read() Method

You can use the .read() method to read the contents of a file into a string:

# f is a file object
file_contents = f.read()

Assignment

python3 main.py

Run and submit the CLI tests.

Tips

  • The path to a file is a string, so it needs to be in quotes.
  • You don't need to write all your functions and variable names the same as mine, I'm just trying to give you some examples of how you can potentially structure your code. There are many correct ways to write the same program.