{ "cells": [ { "cell_type": "markdown", "id": "886a6e03", "metadata": {}, "source": [ "# File I/O\n", "\n", "Files provide a way for us to keep our code focused and not have to worry about having large datasets embedded with the code. This separation also allows using _very_ large datasets as well as use the same code with entirely different datasets (or, as in the case of Homework 3, different images).\n", "\n", "In programming terms, you can think of files as having three different representations:\n", "\n", "1. The **name** or **filepath**, which represents the file's location in the system.\n", "2. A **file object**, which is the programming tool used to access the file.\n", "3. The actual contents of the file.\n", "\n", "Let's go through each in turn." ] }, { "cell_type": "markdown", "id": "84c84d6c", "metadata": {}, "source": [ "## File Names and Paths\n", "\n", "File paths (aka the location of the file on the system/computer) come in two forms:\n", "\n", "1. Absolute\n", "2. Relative\n", "\n", "Both are represented as strings in Python." ] }, { "cell_type": "markdown", "id": "06531ddc", "metadata": {}, "source": [ "* **Absolute Path**: `\"/Users/asfg/uw/25sp/cse160/file_io.ipynb\"`\n", "* **Relative Path**: `\"file_io.ipynb\"`" ] }, { "cell_type": "markdown", "id": "b13b61fd", "metadata": {}, "source": [ "## Opening a file for reading in Python\n", "\n", "1. `open`, read, then `close`\n", "2. `with` ..." ] }, { "cell_type": "code", "execution_count": null, "id": "4863da9a", "metadata": {}, "outputs": [], "source": [ "filepath = \"example.txt\"\n", "file = open(filepath)\n", "\n", "for line in file:\n", " print(line)\n", "\n", "file.close()" ] }, { "cell_type": "code", "execution_count": 1, "id": "85fd905b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is a new line.\n", "\n", "This is another new line.\n" ] } ], "source": [ "filepath = \"example.txt\"\n", "with open(filepath) as file:\n", " for line in file:\n", " print(line)" ] }, { "cell_type": "markdown", "id": "6b6cfc9c", "metadata": {}, "source": [ "### Split and Strip\n", "\n", "Sometimes when we get data from files we need to clean up or separate out the strings.\n", "\n", "* `strip`: removes blank space from the beginning and end of the string.\n", "* `split`: separates the string on the given substring and returns a list of the parts." ] }, { "cell_type": "code", "execution_count": null, "id": "51e3b32e", "metadata": {}, "outputs": [], "source": [ "\" hello world\".strip()" ] }, { "cell_type": "code", "execution_count": null, "id": "032952f6", "metadata": {}, "outputs": [], "source": [ "\" hello world\\n \".strip()" ] }, { "cell_type": "code", "execution_count": null, "id": "ce5c4b54", "metadata": {}, "outputs": [], "source": [ "\"hi there, I am a string with a bunch of spaces\".split()" ] }, { "cell_type": "markdown", "id": "deb5857f", "metadata": {}, "source": [ "## Opening a file for Writing in Python\n", "\n", "Same start as with reading: we must use the `open` function, but specify the `\"w\"` parameter." ] }, { "cell_type": "code", "execution_count": null, "id": "8bcffbfe", "metadata": {}, "outputs": [], "source": [ "filepath = \"example.txt\"\n", "with open(filepath, \"w\") as file:\n", " file.write(\"This is a new line.\\n\")\n", " file.write(\"This is another new line.\\n\")" ] }, { "cell_type": "markdown", "id": "fb62a2f3", "metadata": {}, "source": [ "Note the `\\n`! Without this special indicator, everything will end up on one line in the file. See for example:" ] }, { "cell_type": "code", "execution_count": null, "id": "d47e52ef", "metadata": {}, "outputs": [], "source": [ "filepath = \"example.txt\"\n", "with open(filepath, \"w\") as file:\n", " file.write(\"This is a new line.\")\n", " file.write(\"This is another new line.\")" ] }, { "cell_type": "markdown", "id": "e8ba997d", "metadata": {}, "source": [ "What else do you notice about what happened with the second block?" ] }, { "cell_type": "markdown", "id": "fa7d99d3", "metadata": {}, "source": [ "## Appending instead of overwriting\n", "\n", "We can also open the file for _appending_ using the option `'a'`. This is the same block of code as before, only using `'a'` (**a**ppend) instead `'w'` (**w**rite)." ] }, { "cell_type": "code", "execution_count": null, "id": "38681bd3", "metadata": {}, "outputs": [], "source": [ "filepath = \"example.txt\"\n", "with open(filepath, \"a\") as file:\n", " file.write(\"This is a new line.\")\n", " file.write(\"This is another new line.\")" ] }, { "cell_type": "markdown", "id": "5c4aac73", "metadata": {}, "source": [ "## Reading a file multiple times.\n", "\n", "What happens with this code?" ] }, { "cell_type": "code", "execution_count": null, "id": "e2ee1adc", "metadata": {}, "outputs": [], "source": [ "with open(\"example.txt\") as myfile:\n", " for line_of_text in myfile:\n", " print(line_of_text)\n", "\n", " for line_of_text in myfile:\n", " print(line_of_text)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }