Skip to content

Commit

Permalink
notebook 3: minor changes to first example of file reading
Browse files Browse the repository at this point in the history
  • Loading branch information
robinengler committed Sep 27, 2024
1 parent 1b2177c commit 2c5b746
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions notebooks/03_reading_writing_files.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,53 @@
"\n",
"Once there are no more lines to read, `.readline()` **returns an empty string (`\"\"`)**. This is how we know that the end of the file was reached.\n",
"\n",
"<img src=\"img/file_pointer5.png\" alt=\"a file pointer at the end of the file\" style=\"height:200px;\" />\n",
"<img src=\"img/file_pointer5.png\" alt=\"a file pointer at the end of the file\" style=\"height:200px;\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"<br>\n",
"\n",
"**Let's see an example with actual code:**\n",
"\n",
"Here we are going to read the file `data/fresh_fruits.txt`, which has the following content:\n",
"\n",
"```\n",
"passionfruit\n",
"oranges\n",
"apples\n",
"grapefruit (whole and segments)\n",
"pointed sticks\n",
"```\n",
"\n",
"<br>\n",
"\n",
"* Open the file, and read a single line from it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"data/fresh_fruits.txt\" , mode=\"r\") as reading_handle:\n",
" \n",
" # Read and print the first line in the file.\n",
" line = reading_handle.readline() # Reads a single line from the file.\n",
" print(\"line 1:\", line) # Print the line to the screen.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<br>\n",
"\n",
"**Let's see an example with actual code:**"
"* Open the file, and read all of its content, line by line:"
]
},
{
Expand Down

0 comments on commit 2c5b746

Please sign in to comment.