Skip to content

Commit

Permalink
Merge pull request Pierian-Data#2 from TiVentures/master
Browse files Browse the repository at this point in the history
New content, some fixes
  • Loading branch information
Pierian-Data authored Feb 13, 2018
2 parents 0c07d35 + bb99768 commit 29fec3b
Show file tree
Hide file tree
Showing 24 changed files with 1,962 additions and 22 deletions.

Large diffs are not rendered by default.

124 changes: 114 additions & 10 deletions 00-Python Object and Data Structure Basics/07-Files.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
"source": [
"## Writing to a File\n",
"\n",
"By default, using 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:"
"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:"
]
},
{
Expand All @@ -204,6 +204,7 @@
"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+')"
]
},
Expand All @@ -212,7 +213,7 @@
"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!"
"Opening a file with `'w'` or `'w+'` truncates the original, meaning that anything that was in the original file **is deleted**!"
]
},
{
Expand Down Expand Up @@ -258,18 +259,121 @@
"my_file.read()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"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": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"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": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is a new line\n",
"This is text being appended to test.txt\n",
"And another line here.\n"
]
}
],
"source": [
"my_file.seek(0)\n",
"print(my_file.read())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"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": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Appending to test.txt\n"
]
}
],
"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:"
"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": 12,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -295,7 +399,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand All @@ -317,16 +421,16 @@
"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. Its important to note a few things here:\n",
"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."
"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": 14,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down
Loading

0 comments on commit 29fec3b

Please sign in to comment.