Skip to content

Commit 43df8ea

Browse files
committed
updtae
1 parent 6bfda81 commit 43df8ea

12 files changed

+1904
-32
lines changed

.ipynb_checkpoints/Booleans-checkpoint.ipynb

-6
This file was deleted.
+353-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,357 @@
11
{
2-
"cells": [],
3-
"metadata": {},
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Files\n",
8+
"\n",
9+
"Python uses file objects to interact with external files on your computer. These file objects can be any sort of file you have on your computer, whether it be an audio file, a text file, emails, Excel documents, etc. Note: You will probably need to install certain libraries or modules to interact with those various file types, but they are easily available. (We will cover downloading modules later on in the course).\n",
10+
"\n",
11+
"Python has a built-in open function that allows us to open and play with basic file types. First we will need a file though. We're going to use some iPython magic to create a text file!\n",
12+
"\n",
13+
"## iPython Writing a File"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": 6,
19+
"metadata": {
20+
"collapsed": false
21+
},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"Overwriting test.txt\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"%%writefile test.txt\n",
33+
"Hello, this is a quick test file"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {},
39+
"source": [
40+
"## Python Opening a file\n",
41+
"\n",
42+
"We can open a file with the open() function. The open function also takes in arguments (also called parameters). Lets see how this is used:"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 14,
48+
"metadata": {
49+
"collapsed": true
50+
},
51+
"outputs": [],
52+
"source": [
53+
"# Open the text.txt we made earlier\n",
54+
"my_file = open('test.txt')"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 15,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [
64+
{
65+
"data": {
66+
"text/plain": [
67+
"'Hello, this is a quick test file'"
68+
]
69+
},
70+
"execution_count": 15,
71+
"metadata": {},
72+
"output_type": "execute_result"
73+
}
74+
],
75+
"source": [
76+
"# We can now read the file\n",
77+
"my_file.read()"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 16,
83+
"metadata": {
84+
"collapsed": false
85+
},
86+
"outputs": [
87+
{
88+
"data": {
89+
"text/plain": [
90+
"''"
91+
]
92+
},
93+
"execution_count": 16,
94+
"metadata": {},
95+
"output_type": "execute_result"
96+
}
97+
],
98+
"source": [
99+
"# But what happens if we try to read it again?\n",
100+
"my_file.read()"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"This happens because you can imagine the reading \"cursor\" is at the end of the file after having read it. So there is nothing left to read. We can reset the \"cursor\" like this:"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 42,
113+
"metadata": {
114+
"collapsed": false
115+
},
116+
"outputs": [],
117+
"source": [
118+
"# Seek to the start of file (index 0)\n",
119+
"my_file.seek(0)"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 19,
125+
"metadata": {
126+
"collapsed": false
127+
},
128+
"outputs": [
129+
{
130+
"data": {
131+
"text/plain": [
132+
"'Hello, this is a quick test file'"
133+
]
134+
},
135+
"execution_count": 19,
136+
"metadata": {},
137+
"output_type": "execute_result"
138+
}
139+
],
140+
"source": [
141+
"# Now read again\n",
142+
"my_file.read()"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"In order to not have to reset every time, we can also use the readlines method. Use this with caution ofr large files, since everything will be held in memory. We will learn how to iterate over large files later in the course."
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 26,
155+
"metadata": {
156+
"collapsed": false
157+
},
158+
"outputs": [
159+
{
160+
"data": {
161+
"text/plain": [
162+
"['Hello, this is a quick test file']"
163+
]
164+
},
165+
"execution_count": 26,
166+
"metadata": {},
167+
"output_type": "execute_result"
168+
}
169+
],
170+
"source": [
171+
"# Readlines returns a list of the lines in the file.\n",
172+
"my_file.readlines()"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"## Writing to a File\n",
180+
"\n",
181+
"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:"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 39,
187+
"metadata": {
188+
"collapsed": true
189+
},
190+
"outputs": [],
191+
"source": [
192+
"# Add a second argument to the function, 'w' which stands for write\n",
193+
"my_file = open('test.txt','w+')"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": 40,
199+
"metadata": {
200+
"collapsed": true
201+
},
202+
"outputs": [],
203+
"source": [
204+
"# Write to the file\n",
205+
"my_file.write('This is a new line')"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 43,
211+
"metadata": {
212+
"collapsed": false
213+
},
214+
"outputs": [
215+
{
216+
"data": {
217+
"text/plain": [
218+
"'This is a new line'"
219+
]
220+
},
221+
"execution_count": 43,
222+
"metadata": {},
223+
"output_type": "execute_result"
224+
}
225+
],
226+
"source": [
227+
"# Read the file\n",
228+
"my_file.read()"
229+
]
230+
},
231+
{
232+
"cell_type": "markdown",
233+
"metadata": {},
234+
"source": [
235+
"## Iterating through a File\n",
236+
"\n",
237+
"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:"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": 44,
243+
"metadata": {
244+
"collapsed": false
245+
},
246+
"outputs": [
247+
{
248+
"name": "stdout",
249+
"output_type": "stream",
250+
"text": [
251+
"Overwriting test.txt\n"
252+
]
253+
}
254+
],
255+
"source": [
256+
"%%writefile test.txt\n",
257+
"First Line\n",
258+
"Second Line"
259+
]
260+
},
261+
{
262+
"cell_type": "markdown",
263+
"metadata": {},
264+
"source": [
265+
"Now we can use a little bit of flow to tell the program to for through every line of the file and do something:"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 45,
271+
"metadata": {
272+
"collapsed": false
273+
},
274+
"outputs": [
275+
{
276+
"name": "stdout",
277+
"output_type": "stream",
278+
"text": [
279+
"First Line\n",
280+
"\n",
281+
"Second Line\n"
282+
]
283+
}
284+
],
285+
"source": [
286+
"for line in open('test.txt'):\n",
287+
" print line"
288+
]
289+
},
290+
{
291+
"cell_type": "markdown",
292+
"metadata": {},
293+
"source": [
294+
"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",
295+
"\n",
296+
" 1.) We could have called the 'line' object anything (see example below).\n",
297+
" 2.) By not calling .read() on the file, the whole text file was not stored in memory.\n",
298+
" 3.) Notice the indent on the second line for print. This whitespace is required in Python.\n",
299+
"\n",
300+
"We'll learn a lot more about this later, but up next: Sets and Booleans!"
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": 46,
306+
"metadata": {
307+
"collapsed": false
308+
},
309+
"outputs": [
310+
{
311+
"name": "stdout",
312+
"output_type": "stream",
313+
"text": [
314+
"First Line\n",
315+
"\n",
316+
"Second Line\n"
317+
]
318+
}
319+
],
320+
"source": [
321+
"# Pertaining to the first point above\n",
322+
"for asdf in open('test.txt'):\n",
323+
" print asdf"
324+
]
325+
},
326+
{
327+
"cell_type": "code",
328+
"execution_count": null,
329+
"metadata": {
330+
"collapsed": true
331+
},
332+
"outputs": [],
333+
"source": []
334+
}
335+
],
336+
"metadata": {
337+
"kernelspec": {
338+
"display_name": "Python 2",
339+
"language": "python",
340+
"name": "python2"
341+
},
342+
"language_info": {
343+
"codemirror_mode": {
344+
"name": "ipython",
345+
"version": 2
346+
},
347+
"file_extension": ".py",
348+
"mimetype": "text/x-python",
349+
"name": "python",
350+
"nbconvert_exporter": "python",
351+
"pygments_lexer": "ipython2",
352+
"version": "2.7.10"
353+
}
354+
},
4355
"nbformat": 4,
5356
"nbformat_minor": 0
6357
}

.ipynb_checkpoints/Set-checkpoint.ipynb

-6
This file was deleted.

0 commit comments

Comments
 (0)