5
5
These are simple thing that many computer users already know, but I'll go
6
6
through them just to make sure you know them also.
7
7
8
- #### Files
8
+ ### Files
9
9
10
10
- Each file has a ** name** , like ` hello.py ` , ` mytext.txt ` or
11
11
` coolimage.png ` . Usually the name ends with an ** extension** that
@@ -17,13 +17,13 @@ through them just to make sure you know them also.
17
17
- Files have ** content** that consists of
18
18
[ 8-bit bytes] ( https://www.youtube.com/watch?v=Dnd28lQHquU ) .
19
19
20
- #### Directories/folders
20
+ ### Directories/folders
21
21
22
22
Directories are a way to group files. They also have a name and a location
23
23
like files, but instead of containing data directly like files do they
24
24
contain other files and directories.
25
25
26
- #### Paths
26
+ ### Paths
27
27
28
28
Directories and files have a path, like ` C:\Users\me\hello.py ` . That just
29
29
means that there's a folder called ` C: ` , and inside it there's a folder
@@ -178,7 +178,16 @@ It's also possible to read lines one by one. Files have a
178
178
` readline() ` method that reads the next line, and returns ` '' `
179
179
if we're at the end of the file.
180
180
181
- ** TODO:** example of readline()
181
+ ``` py
182
+ >> > with open (' hello.txt' , ' r' ) as f:
183
+ ... first_line = f.readline()
184
+ ... second_line = f.readline()
185
+ ...
186
+ >> > first_line
187
+ ' Hello one!\n '
188
+ >> > second_line
189
+ ' Hello two!\n '
190
+ ```
182
191
183
192
There's only one confusing thing about reading files. If you try
184
193
to read it twice you'll find out that it only gets read once:
@@ -199,7 +208,10 @@ to read it twice you'll find out that it only gets read once:
199
208
>> >
200
209
```
201
210
202
- But if we open the file again, everything works.
211
+ File objects remember their position. When we tried to read the
212
+ file again it was already at the end, and there was nothing left
213
+ to read. But if we open the file again, it's in the beginning
214
+ again and everything works.
203
215
204
216
``` py
205
217
>> > first = []
@@ -247,7 +259,38 @@ use the `read()` method.
247
259
>> >
248
260
```
249
261
250
- ** TODO:** Explain paths and \\ .
262
+ You can also open full paths, like ` open('C:\\Users\\me', 'r') ` .
263
+ The reason why we need to use ` \\ ` when we really mean ` \ ` is that
264
+ backslash has a special meaning. There are special characters like
265
+ ` \n ` , and ` \\ ` means an actual backslash.
266
+
267
+ ``` py
268
+ >> > print (' C:\some\n ame' )
269
+ C:\some
270
+ ame
271
+ >> > print (' C:\\ some\\ name' )
272
+ C:\some\name
273
+ >> >
274
+ ```
275
+
276
+ Another way to create paths is to tell Python to escape them by
277
+ adding an ` r ` to the beginning of the string. In this case the ` r `
278
+ is short for "raw", not "read".
279
+
280
+ ``` py
281
+ >> > r ' C:\s ome\n ame' == ' C:\\ some\\ name'
282
+ True
283
+ >> >
284
+ ```
285
+
286
+ If you don't use Windows and your paths don't contain backslashes
287
+ you don't need to double anything or use ` r ` in front of paths.
288
+
289
+ ``` py
290
+ >> > print (' /some/name' )
291
+ / some/ name
292
+ >> >
293
+ ```
251
294
252
295
## Example: File viewer
253
296
0 commit comments