

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Running Python
incomplete
2: Book Data
incomplete
3: Read File
incomplete
4: Count Words
incomplete
5: Refactor
incomplete
6: Count Characters
incomplete
This lesson's interactive features are locked, please to keep using them
Now let's start ackshually building BookBot by reading the contents of Frankenstein.
Use a with block with the built-in open() function:
with open(path_to_file) as f:
file_contents = f.read()The f.read() method returns the file's contents as a string.
The with block will automatically close the file when the block exits, cleaning up resources.
python3 main.pyRun and submit the CLI tests from the root of your project.
File paths are strings, so remember to put the path in quotes.
A relative path starts from your current working directory. Because you're running the program from the project root, the path to the book starts with books/.
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.