|
2445 | 2445 | }, |
2446 | 2446 | { |
2447 | 2447 | "cell_type": "code", |
2448 | | - "execution_count": null, |
| 2448 | + "execution_count": 140, |
2449 | 2449 | "metadata": {}, |
2450 | 2450 | "outputs": [], |
2451 | 2451 | "source": [ |
|
2456 | 2456 | }, |
2457 | 2457 | { |
2458 | 2458 | "cell_type": "code", |
2459 | | - "execution_count": null, |
| 2459 | + "execution_count": 141, |
2460 | 2460 | "metadata": {}, |
2461 | | - "outputs": [], |
| 2461 | + "outputs": [ |
| 2462 | + { |
| 2463 | + "data": { |
| 2464 | + "text/plain": [ |
| 2465 | + "76" |
| 2466 | + ] |
| 2467 | + }, |
| 2468 | + "execution_count": 141, |
| 2469 | + "metadata": {}, |
| 2470 | + "output_type": "execute_result" |
| 2471 | + } |
| 2472 | + ], |
2462 | 2473 | "source": [ |
2463 | 2474 | "# [ ] review and run example to write some text to the file\n", |
2464 | | - "new_file.write(\"This is line #1 with 'w'\\nThis is line #2 with 'w'\\nThis is line #3 withn 'w'!\\n\")" |
| 2475 | + "new_file.write(\"This is line #1 with 'w'\\nThis is line #2 with 'w'\\nThis is line #3 with 'w'!\\n\")" |
2465 | 2476 | ] |
2466 | 2477 | }, |
2467 | 2478 | { |
2468 | 2479 | "cell_type": "code", |
2469 | | - "execution_count": null, |
2470 | | - "metadata": { |
2471 | | - "collapsed": true |
2472 | | - }, |
| 2480 | + "execution_count": 142, |
| 2481 | + "metadata": {}, |
2473 | 2482 | "outputs": [], |
2474 | 2483 | "source": [ |
2475 | 2484 | "# [ ] review and run example\n", |
|
2481 | 2490 | }, |
2482 | 2491 | { |
2483 | 2492 | "cell_type": "code", |
2484 | | - "execution_count": null, |
| 2493 | + "execution_count": 143, |
2485 | 2494 | "metadata": { |
2486 | 2495 | "scrolled": true |
2487 | 2496 | }, |
2488 | | - "outputs": [], |
| 2497 | + "outputs": [ |
| 2498 | + { |
| 2499 | + "name": "stdout", |
| 2500 | + "output_type": "stream", |
| 2501 | + "text": [ |
| 2502 | + "This is line #1 with 'w'\n", |
| 2503 | + "This is line #2 with 'w'\n", |
| 2504 | + "This is line #3 with 'w'!\n", |
| 2505 | + "\n" |
| 2506 | + ] |
| 2507 | + } |
| 2508 | + ], |
2489 | 2509 | "source": [ |
2490 | 2510 | "# [ ] review and run example to see what was written to the file\n", |
2491 | 2511 | "new_text = new_file.read()\n", |
|
2523 | 2543 | }, |
2524 | 2544 | { |
2525 | 2545 | "cell_type": "code", |
2526 | | - "execution_count": null, |
| 2546 | + "execution_count": 6, |
2527 | 2547 | "metadata": {}, |
2528 | 2548 | "outputs": [], |
2529 | 2549 | "source": [ |
2530 | 2550 | "# [ ] open planets.txt in write mode\n", |
2531 | | - "\n" |
| 2551 | + "\n", |
| 2552 | + "planets = open('inner_planets.txt', 'w')" |
2532 | 2553 | ] |
2533 | 2554 | }, |
2534 | 2555 | { |
2535 | 2556 | "cell_type": "code", |
2536 | | - "execution_count": null, |
| 2557 | + "execution_count": 7, |
2537 | 2558 | "metadata": {}, |
2538 | | - "outputs": [], |
| 2559 | + "outputs": [ |
| 2560 | + { |
| 2561 | + "data": { |
| 2562 | + "text/plain": [ |
| 2563 | + "24" |
| 2564 | + ] |
| 2565 | + }, |
| 2566 | + "execution_count": 7, |
| 2567 | + "metadata": {}, |
| 2568 | + "output_type": "execute_result" |
| 2569 | + } |
| 2570 | + ], |
2539 | 2571 | "source": [ |
2540 | 2572 | "# [ ] write Mercury, Venus, Earth, Mars on separate lines\n", |
2541 | | - "\n" |
| 2573 | + "\n", |
| 2574 | + "planets.write(\"Mercury\\nVenus\\nEarth\\nMars\")" |
2542 | 2575 | ] |
2543 | 2576 | }, |
2544 | 2577 | { |
2545 | 2578 | "cell_type": "code", |
2546 | | - "execution_count": null, |
| 2579 | + "execution_count": 8, |
2547 | 2580 | "metadata": {}, |
2548 | 2581 | "outputs": [], |
2549 | 2582 | "source": [ |
2550 | 2583 | "# [ ] close the file and re-open in read mode\n", |
2551 | | - "\n" |
| 2584 | + "\n", |
| 2585 | + "planets.close()\n", |
| 2586 | + "planets = open('inner_planets.txt', 'r')" |
2552 | 2587 | ] |
2553 | 2588 | }, |
2554 | 2589 | { |
2555 | 2590 | "cell_type": "code", |
2556 | | - "execution_count": null, |
2557 | | - "metadata": { |
2558 | | - "collapsed": true |
2559 | | - }, |
2560 | | - "outputs": [], |
| 2591 | + "execution_count": 9, |
| 2592 | + "metadata": {}, |
| 2593 | + "outputs": [ |
| 2594 | + { |
| 2595 | + "data": { |
| 2596 | + "text/plain": [ |
| 2597 | + "'Mercury\\nVenus\\nEarth\\nMars'" |
| 2598 | + ] |
| 2599 | + }, |
| 2600 | + "execution_count": 9, |
| 2601 | + "metadata": {}, |
| 2602 | + "output_type": "execute_result" |
| 2603 | + } |
| 2604 | + ], |
2561 | 2605 | "source": [ |
2562 | 2606 | "# [ ] use .read() to read the entire file contents\n", |
2563 | | - "\n" |
| 2607 | + "\n", |
| 2608 | + "planets.read()" |
2564 | 2609 | ] |
2565 | 2610 | }, |
2566 | 2611 | { |
2567 | 2612 | "cell_type": "code", |
2568 | | - "execution_count": null, |
| 2613 | + "execution_count": 10, |
2569 | 2614 | "metadata": {}, |
2570 | | - "outputs": [], |
| 2615 | + "outputs": [ |
| 2616 | + { |
| 2617 | + "name": "stdout", |
| 2618 | + "output_type": "stream", |
| 2619 | + "text": [ |
| 2620 | + "\n" |
| 2621 | + ] |
| 2622 | + } |
| 2623 | + ], |
2571 | 2624 | "source": [ |
2572 | 2625 | "# [ ] print the entire file contents and close the file\n", |
2573 | | - "\n" |
| 2626 | + "\n", |
| 2627 | + "planetsft = planets.read()\n", |
| 2628 | + "print(planetsft)\n", |
| 2629 | + "\n", |
| 2630 | + "planets.close()" |
2574 | 2631 | ] |
2575 | 2632 | }, |
2576 | 2633 | { |
|
0 commit comments