Files
labs/Module-2/1.Basic/08-Files.ipynb
T

389 lines
8.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile test.txt\n",
"Hello, this is a quick test file."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python Opening a file%%writefile test.txt\n",
"Hello, this is a quick test file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"myfile = open('whoops.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To avoid this error,make sure your .txt file is saved in the same location as your notebook, to check your notebook location, use **pwd**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pwd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Alternatively, to grab files from any location on your computer, simply pass in the entire file path. **\n",
"\n",
"For Windows you need to use double \\ so python doesn't treat the second \\ as an escape character, a file path is in the form:\n",
"\n",
" myfile = open(\"C:\\\\Users\\\\YourUserName\\\\Home\\\\Folder\\\\myfile.txt\")\n",
"\n",
"For MacOS and Linux you use slashes in the opposite direction:\n",
"\n",
" myfile = open(\"/Users/YouUserName/Folder/myfile.txt\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Open the text.txt we made earlier\n",
"my_file = open('test.txt')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# We can now read the file\n",
"my_file.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# But what happens if we try to read it again?\n",
"my_file.read()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This happens because you can imagine the reading \"cursor\" is at the end of the file after having read it. So there is nothing left to read. We can reset the \"cursor\" like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Seek to the start of file (index 0)\n",
"my_file.seek(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now read again\n",
"my_file.read()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can read a file line by line using the readlines method. Use caution with large files, since everything will be held in memory. We will learn how to iterate over large files later in the course."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Readlines returns a list of the lines in the file\n",
"my_file.seek(0)\n",
"my_file.readlines()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you have finished using a file, it is always good practice to close it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"my_file.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Writing to a File\n",
"\n",
"By default, the `open()` function will only allow us to read the file. We need to pass the argument `'w'` to write over the file. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Add a second argument to the function, 'w' which stands for write.\n",
"# Passing 'w+' lets us read and write to the file\n",
"\n",
"my_file = open('test.txt','w+')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### <strong><font color='red'>Use caution!</font></strong> \n",
"Opening a file with `'w'` or `'w+'` truncates the original, meaning that anything that was in the original file **is deleted**!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Write to the file\n",
"my_file.write('This is a new line')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read the file\n",
"my_file.seek(0)\n",
"my_file.read()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"my_file.close() # always do this when you're done with a file"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Appending to a File\n",
"Passing the argument `'a'` opens the file and puts the pointer at the end, so anything written is appended. Like `'w+'`, `'a+'` lets us read and write to a file. If the file does not exist, one will be created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_file = open('test.txt','a+')\n",
"my_file.write('\\nThis is text being appended to test.txt')\n",
"my_file.write('\\nAnd another line here.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_file.seek(0)\n",
"print(my_file.read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"my_file.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Appending with `%%writefile`\n",
"We can do the same thing using IPython cell magic:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile -a test.txt\n",
"\n",
"This is text being appended to test.txt\n",
"And another line here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add a blank space if you want the first line to begin on its own line, as Jupyter won't recognize escape sequences like `\\n`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterating through a File\n",
"\n",
"Lets get a quick preview of a for loop by iterating over a text file. First let's make a new text file with some IPython Magic:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile test.txt\n",
"First Line\n",
"Second Line"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use a little bit of flow to tell the program to for through every line of the file and do something:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for line in open('test.txt'):\n",
" print(line)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Don't worry about fully understanding this yet, for loops are coming up soon. But we'll break down what we did above. We said that for every line in this text file, go ahead and print that line. It's important to note a few things here:\n",
"\n",
"1. We could have called the \"line\" object anything (see example below).\n",
"2. By not calling `.read()` on the file, the whole text file was not stored in memory.\n",
"3. Notice the indent on the second line for print. This whitespace is required in Python."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Pertaining to the first point above\n",
"for asdf in open('test.txt'):\n",
" print(asdf)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll learn a lot more about this later, but up next: Sets and Booleans!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}