-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path03-FileIO.html
117 lines (106 loc) · 40.6 KB
/
03-FileIO.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>03-FileIO</title>
<script type="text/javascript" src="03-FileIO_files/MathJax.js" charset="utf-8"></script>
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "mathjax/MathJax.js";
</script>
<link rel="stylesheet" href="03-FileIO_files/jquery-wijmo.css" type="text/css">
<link rel="stylesheet" href="03-FileIO_files/codemirror.css">
<link rel="stylesheet" href="03-FileIO_files/markdown.css">
<link rel="stylesheet" href="03-FileIO_files/rst.css">
<link rel="stylesheet" href="03-FileIO_files/ipython.css">
<link rel="stylesheet" href="03-FileIO_files/default.css">
<link rel="stylesheet" href="03-FileIO_files/prettify.css">
<link rel="stylesheet" href="03-FileIO_files/boilerplate.css" type="text/css">
<link rel="stylesheet" href="03-FileIO_files/layout.css" type="text/css">
<link rel="stylesheet" href="03-FileIO_files/base.css" type="text/css">
<link rel="stylesheet" href="03-FileIO_files/notebook.css" type="text/css">
<link rel="stylesheet" href="03-FileIO_files/renderedhtml.css" type="text/css">
<meta name="read_only" content="False">
<style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style><style type="text/css">@media print { body { overflow: visible !important; } }.ui-widget-content { border: 0px; }</style><style type="text/css">#MathJax_Zoom {position: absolute; background-color: #F0F0F0; overflow: auto; display: block; z-index: 301; padding: .5em; border: 1px solid black; margin: 0; font-family: serif; font-size: 85%; font-weight: normal; font-style: normal; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; box-shadow: 5px 5px 15px #AAAAAA; -webkit-box-shadow: 5px 5px 15px #AAAAAA; -moz-box-shadow: 5px 5px 15px #AAAAAA; -khtml-box-shadow: 5px 5px 15px #AAAAAA; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
#MathJax_ZoomOverlay {position: absolute; left: 0; top: 0; z-index: 300; display: inline-block; width: 100%; height: 100%; border: 0; padding: 0; margin: 0; background-color: white; opacity: 0; filter: alpha(opacity=0)}
</style><style type="text/css">#MathJax_About {position: fixed; left: 50%; width: auto; text-align: center; border: 3px outset; padding: 1em 2em; background-color: #DDDDDD; cursor: default; font-family: message-box; font-size: 120%; font-style: normal; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 15px; -webkit-border-radius: 15px; -moz-border-radius: 15px; -khtml-border-radius: 15px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_Menu {position: absolute; background-color: white; color: black; width: auto; padding: 5px 0px; border: 1px solid #CCCCCC; margin: 0; cursor: default; font: menu; text-align: left; text-indent: 0; text-transform: none; line-height: normal; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; z-index: 201; border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; -khtml-border-radius: 5px; box-shadow: 0px 10px 20px #808080; -webkit-box-shadow: 0px 10px 20px #808080; -moz-box-shadow: 0px 10px 20px #808080; -khtml-box-shadow: 0px 10px 20px #808080; filter: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
.MathJax_MenuItem {padding: 1px 2em; background: transparent}
.MathJax_MenuTitle {background-color: #CCCCCC; margin: -5px 0 0 0; text-align: center; font-style: italic; font-size: 80%; color: #444444; padding: 2px 0; overflow: hidden}
.MathJax_MenuArrow {position: absolute; right: .5em; color: #666666}
.MathJax_MenuActive .MathJax_MenuArrow {color: white}
.MathJax_MenuCheck {position: absolute; left: .7em}
.MathJax_MenuRadioCheck {position: absolute; left: .7em}
.MathJax_MenuLabel {padding: 1px 2em 3px 1.33em; font-style: italic}
.MathJax_MenuRule {border-top: 1px solid #DDDDDD; margin: 4px 3px}
.MathJax_MenuDisabled {color: GrayText}
.MathJax_MenuActive {background-color: #606872; color: white}
</style><style type="text/css">#MathJax_Message {position: fixed; left: 1px; bottom: 2px; background-color: #E6E6E6; border: 1px solid #959595; margin: 0px; padding: 2px 8px; z-index: 102; color: black; font-size: 80%; width: auto; white-space: nowrap}
#MathJax_MSIE_Frame {position: absolute; top: 0; left: 0; width: 0px; z-index: 101; border: 0px; margin: 0px; padding: 0px}
.MathJax_Error {color: #CC0000; font-style: italic}
</style></head><body style="overflow: auto;"><div tabindex="2" class="cell text_cell border-box-sizing ui-widget-content ui-corner-all"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 1px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h1>File I/O</h1></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [14]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/etc/hosts')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px; visibility: hidden;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/etc/hosts'</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>127.0.0.1 localhost
127.0.1.1 precise64
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [15]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 100px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/etc/hosts')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px; visibility: hidden;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/etc/hosts'</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">repr</span><span class="cm-null">(</span><span class="cm-variable">fp.read</span><span class="cm-null">(</span><span class="cm-number">40</span><span class="cm-null">))</span></pre><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">repr</span><span class="cm-null">(</span><span class="cm-variable">fp.read</span><span class="cm-null">(</span><span class="cm-number">40</span><span class="cm-null">))</span></pre><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">repr</span><span class="cm-null">(</span><span class="cm-variable">fp.read</span><span class="cm-null">(</span><span class="cm-number">40</span><span class="cm-null">))</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>'127.0.0.1\tlocalhost\n127.0.1.1\tprecise64\n'
'\n# The following lines are desirable for'
' IPv6 capable hosts\n::1 ip6-localhos'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [16]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/etc/hosts')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/etc/hosts'</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">for</span><span class="cm-null"> </span><span class="cm-variable">line</span><span class="cm-null"> </span><span class="cm-operator">in</span><span class="cm-null"> </span><span class="cm-variable">fp</span><span class="cm-null">:</span></pre><pre><span class="cm-null"> </span><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">repr</span><span class="cm-null">(</span><span class="cm-variable">line</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>'127.0.0.1\tlocalhost\n'
'127.0.1.1\tprecise64\n'
'\n'
'# The following lines are desirable for IPv6 capable hosts\n'
'::1 ip6-localhost ip6-loopback\n'
'fe00::0 ip6-localnet\n'
'ff00::0 ip6-mcastprefix\n'
'ff02::1 ip6-allnodes\n'
'ff02::2 ip6-allrouters\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [17]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/etc/hosts')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/etc/hosts'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.readlines</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[17]:</div><div class="box_flex1 output_subarea output_text"><pre>['127.0.0.1\tlocalhost\n',
'127.0.1.1\tprecise64\n',
'\n',
'# The following lines are desirable for IPv6 capable hosts\n',
'::1 ip6-localhost ip6-loopback\n',
'fe00::0 ip6-localnet\n',
'ff00::0 ip6-mcastprefix\n',
'ff02::1 ip6-allnodes\n',
'ff02::2 ip6-allrouters\n']</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [18]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>print fp.read()</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre></pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [20]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>print fp.read()</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">10</span><span class="cm-null">)</span></pre><pre><span class="cm-keyword">print</span><span class="cm-null"> </span><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text output_stream output_stdout"><pre>localhost
127.0.1.1 precise64
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [21]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp.tell()</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp.tell</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[21]:</div><div class="box_flex1 output_subarea output_text"><pre>224</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [22]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 65px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'w')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'w'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'abcd\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: none;" class="output vbox"></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [25]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 47px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[25]:</div><div class="box_flex1 output_subarea output_text"><pre>'abcd\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [26]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 30px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp.write('hi')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'hi'</span><span class="cm-null">)</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text"><pre><span class="ansired">---------------------------------------------------------------------------</span>
<span class="ansired">IOError</span> Traceback (most recent call last)
<span class="ansigreen">/vagrant/<ipython-input-26-40576b043654></span> in <span class="ansicyan"><module></span><span class="ansiblue">()</span>
<span class="ansigreen">----> 1</span><span class="ansired"> </span>fp<span class="ansiblue">.</span>write<span class="ansiblue">(</span><span class="ansiblue">'hi'</span><span class="ansiblue">)</span><span class="ansiblue"></span>
<span class="ansired">IOError</span>: File not open for writing
</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [28]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'a')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'a'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'hi\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">)</span><span class="cm-variable">.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[28]:</div><div class="box_flex1 output_subarea output_text"><pre>'abcd\nhi\nhi\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [29]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'w')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'w'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'hi\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.close</span><span class="cm-null">()</span></pre><pre><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">)</span><span class="cm-variable">.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[29]:</div><div class="box_flex1 output_subarea output_text"><pre>'hi\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [31]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'w')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'w'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'hi\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">0</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt"></div><div class="box_flex1 output_subarea output_text"><pre><span class="ansired">---------------------------------------------------------------------------</span>
<span class="ansired">IOError</span> Traceback (most recent call last)
<span class="ansigreen">/vagrant/<ipython-input-31-6bf0db77664c></span> in <span class="ansicyan"><module></span><span class="ansiblue">()</span>
<span class="ansigreen"> 2</span> fp<span class="ansiblue">.</span>write<span class="ansiblue">(</span><span class="ansiblue">'hi\n'</span><span class="ansiblue">)</span><span class="ansiblue"></span>
<span class="ansigreen"> 3</span> fp<span class="ansiblue">.</span>seek<span class="ansiblue">(</span><span class="ansicyan">0</span><span class="ansiblue">)</span><span class="ansiblue"></span>
<span class="ansigreen">----> 4</span><span class="ansired"> </span>fp<span class="ansiblue">.</span>read<span class="ansiblue">(</span><span class="ansiblue">)</span><span class="ansiblue"></span>
<span class="ansired">IOError</span>: File not open for reading
</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [40]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'w+')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'w+'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'hi\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">0</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[40]:</div><div class="box_flex1 output_subarea output_text"><pre>'hi\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [41]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 100px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'r+')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'r+'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">2</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'there\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">0</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[41]:</div><div class="box_flex1 output_subarea output_text"><pre>'hithere\n'</pre></div></div></div></div><div tabindex="2" class="cell border-box-sizing code_cell vbox"><div class="input hbox"><div class="prompt input_prompt">In [42]:</div><div class="input_area box-flex1"><div class="CodeMirror CodeMirror-focused"><div style="overflow: hidden; position: relative; width: 1px; height: 0px; top: 0px; left: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-ipython"><div style="position: relative; height: 82px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre><span>fp = open('/tmp/testfile.txt', 'a+')</span></pre></div><div style="position: relative; top: 0px;"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative;" draggable="true"><pre style="top: 0px; left: 0px;" class="CodeMirror-cursor"> </pre><div style=""><pre><span class="cm-variable">fp</span><span class="cm-null"> = </span><span class="cm-variable">open</span><span class="cm-null">(</span><span class="cm-string">'/tmp/testfile.txt'</span><span class="cm-null">, </span><span class="cm-string">'a+'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.write</span><span class="cm-null">(</span><span class="cm-string">'again\n'</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.seek</span><span class="cm-null">(</span><span class="cm-number">0</span><span class="cm-null">)</span></pre><pre><span class="cm-variable">fp.read</span><span class="cm-null">()</span></pre></div></div></div></div></div></div></div></div></div><div style="display: -moz-box;" class="output vbox"><div class="hbox output_area"><div class="prompt output_prompt">Out[42]:</div><div class="box_flex1 output_subarea output_text"><pre>'hithere\nagain\n'</pre></div></div></div></div><div tabindex="2" class="cell text_cell border-box-sizing"><div style="display: none;" class="text_cell_input"><div class="CodeMirror"><div style="overflow: hidden; position: relative; width: 1px; height: 0px;"><textarea style="position: absolute; width: 2px;" wrap="off" autocorrect="off" autocapitalize="off"></textarea></div><div class="CodeMirror-scroll cm-s-default"><div style="position: relative; height: 4px;"><div style="position: absolute; height: 0; width: 0; overflow: hidden;"><pre>x</pre></div><div style="position: relative"><div style="display: none;" class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div><div class="CodeMirror-lines"><div style="position: relative" draggable="true"><pre class="CodeMirror-cursor"> </pre><div></div></div></div></div></div></div></div></div><div tabindex="-1" class="text_cell_render rendered_html"><h3>Exercises</h3>
<ul>
<li>Write a function that will print out a text file, line by line</li>
<li>Enhance the function to print a line number before each line</li>
</ul></div></div><div style="height: 30%;" class="end_space"></div></body></html>