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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
{
"metadata": {
"name": "",
"signature": "sha256:3748f84e3bd0279efe47855bd2c4e23912ef725121f3591980fa33e19d6ccae4"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 14, Input/Output and Files"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 14-5, Page Number: 424"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Files : Opening and Closing\n",
"\n",
"# Open the file for writing\n",
"\n",
"file_pointer = open(\"first_file\",\"w\")\n",
"\n",
"print \"Enter text:\"\n",
"\n",
"# Get characters and place them in the file\n",
"\n",
"text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n",
"print text\n",
"file_pointer.write(text)\n",
"\n",
"file_pointer.close()\n",
"\n",
"file_pointer = open(\"first_file\",\"r\")\n",
"\n",
"#Display the text from file\n",
"\n",
"print \"\\nYou entered:\"\n",
"print file_pointer.read()\n",
"\n",
"file_pointer.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter text:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n",
"\n",
"You entered:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 14-6, Page Number: 426"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using several files\n",
"\n",
"# Open the source file for reading\n",
"\n",
"first_file_pointer = open(\"first_file\",\"r\")\n",
"\n",
"# Open the destination file for writing\n",
"\n",
"second_file_pointer = open(\"second_file\",\"w\")\n",
"\n",
"# Transfer characters from source to destination file\n",
"\n",
"c = first_file_pointer.read()\n",
"second_file_pointer.write(c)\n",
"\n",
"# Close both files\n",
"\n",
"first_file_pointer.close()\n",
"second_file_pointer.close()\n",
"\n",
"# Open second file for displaying contentes\n",
"\n",
"second_file_pointer = open(\"second_file\",\"r\")\n",
"print(\"'second_file' contains:\")\n",
"print second_file_pointer.read()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"'second_file' contains:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 14-7, Page Number : 428"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Append mode\n",
"\n",
"# Open the first file for writing\n",
"\n",
"first_file_pointer = open(\"first_file\",\"w\")\n",
"\n",
"# Open the second file for appending\n",
"\n",
"second_file_pointer = open(\"second_file\",\"a\")\n",
"\n",
"# Writing to both files\n",
"\n",
"first_file_pointer.write(\"\\n--Anonymous\\n\")\n",
"second_file_pointer.write(\"\\n--Anonymous\\n\")\n",
"\n",
"# Close both files\n",
"\n",
"first_file_pointer.close()\n",
"second_file_pointer.close()\n",
"\n",
"# Display the contents of first file\n",
"first_file_pointer = open(\"first_file\",\"r\")\n",
"print(\"After write, 'first_file contains:\")\n",
"print first_file_pointer.read()\n",
"\n",
"# Display the contents of second file\n",
"second_file_pointer = open(\"second_file\",\"r\")\n",
"print(\"After append, 'second_file' contains:\")\n",
"print second_file_pointer.read()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"After write, 'first_file contains:\n",
"\n",
"--Anonymous\n",
"\n",
"After append, 'second_file' contains:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n",
"--Anonymous\n",
"\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 14-8, Page Number: 431"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The seek function\n",
"\n",
"# Open file for reading\n",
"try :\n",
"\tfile_pointer = open(\"second_file\",\"r\")\n",
"except :\n",
"\tprint \"'second_file' cannot be opened\"\n",
"\texit ()\n",
"\n",
"# Skip the first 6 characters\n",
"\n",
"file_pointer.seek(6,0)\n",
"print file_pointer.read()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n",
"--Anonymous\n",
"\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Program 14-9, Page Number: 433"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# The 'w+' mode and seek to rewind a file\n",
"\n",
"# Open a file for read/write\n",
"file_pointer = open(\"first_file\",\"w+\")\n",
"\n",
"print \"Enter text:\"\n",
"text = \"BASIC is easy,\\nFORTAN is fun,\\nPascal may be structured,\\nBut C's the one!\"\n",
"print text\n",
"file_pointer.write(text)\n",
"\n",
"file_pointer.seek(0,0)\n",
"\n",
"# Display the file\n",
"print \"\\nYou entered:\"\n",
"print file_pointer.read()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter text:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n",
"\n",
"You entered:\n",
"BASIC is easy,\n",
"FORTAN is fun,\n",
"Pascal may be structured,\n",
"But C's the one!\n"
]
}
],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
|