Skip to content

Commit 29fec3b

Browse files
authored
Merge pull request Pierian-Data#2 from TiVentures/master
New content, some fixes
2 parents 0c07d35 + bb99768 commit 29fec3b

24 files changed

+1962
-22
lines changed

00-Python Object and Data Structure Basics/.ipynb_checkpoints/07-Files-checkpoint.ipynb

Lines changed: 481 additions & 0 deletions
Large diffs are not rendered by default.

00-Python Object and Data Structure Basics/07-Files.ipynb

Lines changed: 114 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
"source": [
194194
"## Writing to a File\n",
195195
"\n",
196-
"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:"
196+
"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:"
197197
]
198198
},
199199
{
@@ -204,6 +204,7 @@
204204
"source": [
205205
"# Add a second argument to the function, 'w' which stands for write.\n",
206206
"# Passing 'w+' lets us read and write to the file\n",
207+
"\n",
207208
"my_file = open('test.txt','w+')"
208209
]
209210
},
@@ -212,7 +213,7 @@
212213
"metadata": {},
213214
"source": [
214215
"### <strong><font color='red'>Use caution!</font></strong> \n",
215-
"Opening a file with 'w' or 'w+' truncates the original, meaning that anything that was in the original file is deleted!"
216+
"Opening a file with `'w'` or `'w+'` truncates the original, meaning that anything that was in the original file **is deleted**!"
216217
]
217218
},
218219
{
@@ -258,18 +259,121 @@
258259
"my_file.read()"
259260
]
260261
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": 12,
265+
"metadata": {},
266+
"outputs": [],
267+
"source": [
268+
"my_file.close() # always do this when you're done with a file"
269+
]
270+
},
271+
{
272+
"cell_type": "markdown",
273+
"metadata": {},
274+
"source": [
275+
"## Appending to a File\n",
276+
"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."
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": 13,
282+
"metadata": {},
283+
"outputs": [
284+
{
285+
"data": {
286+
"text/plain": [
287+
"23"
288+
]
289+
},
290+
"execution_count": 13,
291+
"metadata": {},
292+
"output_type": "execute_result"
293+
}
294+
],
295+
"source": [
296+
"my_file = open('test.txt','a+')\n",
297+
"my_file.write('\\nThis is text being appended to test.txt')\n",
298+
"my_file.write('\\nAnd another line here.')"
299+
]
300+
},
301+
{
302+
"cell_type": "code",
303+
"execution_count": 14,
304+
"metadata": {},
305+
"outputs": [
306+
{
307+
"name": "stdout",
308+
"output_type": "stream",
309+
"text": [
310+
"This is a new line\n",
311+
"This is text being appended to test.txt\n",
312+
"And another line here.\n"
313+
]
314+
}
315+
],
316+
"source": [
317+
"my_file.seek(0)\n",
318+
"print(my_file.read())"
319+
]
320+
},
321+
{
322+
"cell_type": "code",
323+
"execution_count": 15,
324+
"metadata": {},
325+
"outputs": [],
326+
"source": [
327+
"my_file.close()"
328+
]
329+
},
330+
{
331+
"cell_type": "markdown",
332+
"metadata": {},
333+
"source": [
334+
"### Appending with `%%writefile`\n",
335+
"We can do the same thing using IPython cell magic:"
336+
]
337+
},
338+
{
339+
"cell_type": "code",
340+
"execution_count": 16,
341+
"metadata": {},
342+
"outputs": [
343+
{
344+
"name": "stdout",
345+
"output_type": "stream",
346+
"text": [
347+
"Appending to test.txt\n"
348+
]
349+
}
350+
],
351+
"source": [
352+
"%%writefile -a test.txt\n",
353+
"\n",
354+
"This is text being appended to test.txt\n",
355+
"And another line here."
356+
]
357+
},
358+
{
359+
"cell_type": "markdown",
360+
"metadata": {},
361+
"source": [
362+
"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`"
363+
]
364+
},
261365
{
262366
"cell_type": "markdown",
263367
"metadata": {},
264368
"source": [
265369
"## Iterating through a File\n",
266370
"\n",
267-
"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:"
371+
"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:"
268372
]
269373
},
270374
{
271375
"cell_type": "code",
272-
"execution_count": 12,
376+
"execution_count": 17,
273377
"metadata": {},
274378
"outputs": [
275379
{
@@ -295,7 +399,7 @@
295399
},
296400
{
297401
"cell_type": "code",
298-
"execution_count": 13,
402+
"execution_count": 18,
299403
"metadata": {},
300404
"outputs": [
301405
{
@@ -317,16 +421,16 @@
317421
"cell_type": "markdown",
318422
"metadata": {},
319423
"source": [
320-
"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",
424+
"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",
321425
"\n",
322-
" 1.) We could have called the 'line' object anything (see example below).\n",
323-
" 2.) By not calling .read() on the file, the whole text file was not stored in memory.\n",
324-
" 3.) Notice the indent on the second line for print. This whitespace is required in Python."
426+
"1. We could have called the \"line\" object anything (see example below).\n",
427+
"2. By not calling `.read()` on the file, the whole text file was not stored in memory.\n",
428+
"3. Notice the indent on the second line for print. This whitespace is required in Python."
325429
]
326430
},
327431
{
328432
"cell_type": "code",
329-
"execution_count": 14,
433+
"execution_count": 19,
330434
"metadata": {},
331435
"outputs": [
332436
{

0 commit comments

Comments
 (0)