|
193 | 193 | "source": [
|
194 | 194 | "## Writing to a File\n",
|
195 | 195 | "\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:" |
197 | 197 | ]
|
198 | 198 | },
|
199 | 199 | {
|
|
204 | 204 | "source": [
|
205 | 205 | "# Add a second argument to the function, 'w' which stands for write.\n",
|
206 | 206 | "# Passing 'w+' lets us read and write to the file\n",
|
| 207 | + "\n", |
207 | 208 | "my_file = open('test.txt','w+')"
|
208 | 209 | ]
|
209 | 210 | },
|
|
212 | 213 | "metadata": {},
|
213 | 214 | "source": [
|
214 | 215 | "### <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**!" |
216 | 217 | ]
|
217 | 218 | },
|
218 | 219 | {
|
|
258 | 259 | "my_file.read()"
|
259 | 260 | ]
|
260 | 261 | },
|
| 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 | + }, |
261 | 365 | {
|
262 | 366 | "cell_type": "markdown",
|
263 | 367 | "metadata": {},
|
264 | 368 | "source": [
|
265 | 369 | "## Iterating through a File\n",
|
266 | 370 | "\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:" |
268 | 372 | ]
|
269 | 373 | },
|
270 | 374 | {
|
271 | 375 | "cell_type": "code",
|
272 |
| - "execution_count": 12, |
| 376 | + "execution_count": 17, |
273 | 377 | "metadata": {},
|
274 | 378 | "outputs": [
|
275 | 379 | {
|
|
295 | 399 | },
|
296 | 400 | {
|
297 | 401 | "cell_type": "code",
|
298 |
| - "execution_count": 13, |
| 402 | + "execution_count": 18, |
299 | 403 | "metadata": {},
|
300 | 404 | "outputs": [
|
301 | 405 | {
|
|
317 | 421 | "cell_type": "markdown",
|
318 | 422 | "metadata": {},
|
319 | 423 | "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", |
321 | 425 | "\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." |
325 | 429 | ]
|
326 | 430 | },
|
327 | 431 | {
|
328 | 432 | "cell_type": "code",
|
329 |
| - "execution_count": 14, |
| 433 | + "execution_count": 19, |
330 | 434 | "metadata": {},
|
331 | 435 | "outputs": [
|
332 | 436 | {
|
|
0 commit comments