Skip to content

Commit 2a66bfd

Browse files
committed
Finished task 14 in P2M4 Tutorials
1 parent 8ba4deb commit 2a66bfd

4 files changed

Lines changed: 94 additions & 26 deletions

File tree

Python Fundamentals/Module_4.0_Tutorials_Files_Python_Fundamentals.ipynb

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,7 +2445,7 @@
24452445
},
24462446
{
24472447
"cell_type": "code",
2448-
"execution_count": null,
2448+
"execution_count": 140,
24492449
"metadata": {},
24502450
"outputs": [],
24512451
"source": [
@@ -2456,20 +2456,29 @@
24562456
},
24572457
{
24582458
"cell_type": "code",
2459-
"execution_count": null,
2459+
"execution_count": 141,
24602460
"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+
],
24622473
"source": [
24632474
"# [ ] 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\")"
24652476
]
24662477
},
24672478
{
24682479
"cell_type": "code",
2469-
"execution_count": null,
2470-
"metadata": {
2471-
"collapsed": true
2472-
},
2480+
"execution_count": 142,
2481+
"metadata": {},
24732482
"outputs": [],
24742483
"source": [
24752484
"# [ ] review and run example\n",
@@ -2481,11 +2490,22 @@
24812490
},
24822491
{
24832492
"cell_type": "code",
2484-
"execution_count": null,
2493+
"execution_count": 143,
24852494
"metadata": {
24862495
"scrolled": true
24872496
},
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+
],
24892509
"source": [
24902510
"# [ ] review and run example to see what was written to the file\n",
24912511
"new_text = new_file.read()\n",
@@ -2523,54 +2543,91 @@
25232543
},
25242544
{
25252545
"cell_type": "code",
2526-
"execution_count": null,
2546+
"execution_count": 6,
25272547
"metadata": {},
25282548
"outputs": [],
25292549
"source": [
25302550
"# [ ] open planets.txt in write mode\n",
2531-
"\n"
2551+
"\n",
2552+
"planets = open('inner_planets.txt', 'w')"
25322553
]
25332554
},
25342555
{
25352556
"cell_type": "code",
2536-
"execution_count": null,
2557+
"execution_count": 7,
25372558
"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+
],
25392571
"source": [
25402572
"# [ ] write Mercury, Venus, Earth, Mars on separate lines\n",
2541-
"\n"
2573+
"\n",
2574+
"planets.write(\"Mercury\\nVenus\\nEarth\\nMars\")"
25422575
]
25432576
},
25442577
{
25452578
"cell_type": "code",
2546-
"execution_count": null,
2579+
"execution_count": 8,
25472580
"metadata": {},
25482581
"outputs": [],
25492582
"source": [
25502583
"# [ ] 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')"
25522587
]
25532588
},
25542589
{
25552590
"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+
],
25612605
"source": [
25622606
"# [ ] use .read() to read the entire file contents\n",
2563-
"\n"
2607+
"\n",
2608+
"planets.read()"
25642609
]
25652610
},
25662611
{
25672612
"cell_type": "code",
2568-
"execution_count": null,
2613+
"execution_count": 10,
25692614
"metadata": {},
2570-
"outputs": [],
2615+
"outputs": [
2616+
{
2617+
"name": "stdout",
2618+
"output_type": "stream",
2619+
"text": [
2620+
"\n"
2621+
]
2622+
}
2623+
],
25712624
"source": [
25722625
"# [ ] 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()"
25742631
]
25752632
},
25762633
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Mercury
2+
Venus
3+
Earth
4+
Mars

Python Fundamentals/new_file.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is line #1 with 'w'
2+
This is line #2 with 'w'
3+
This is line #3 with 'w'!

Python Fundamentals/planets.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Mercury
2+
Venus
3+
Earth
4+
Mars

0 commit comments

Comments
 (0)