55These are simple thing that many computer users already know, but I'll go
66through them just to make sure you know them also.
77
8- #### Files
8+ ### Files
99
1010- Each file has a ** name** , like ` hello.py ` , ` mytext.txt ` or
1111 ` coolimage.png ` . Usually the name ends with an ** extension** that
@@ -17,13 +17,13 @@ through them just to make sure you know them also.
1717- Files have ** content** that consists of
1818 [ 8-bit bytes] ( https://www.youtube.com/watch?v=Dnd28lQHquU ) .
1919
20- #### Directories/folders
20+ ### Directories/folders
2121
2222Directories are a way to group files. They also have a name and a location
2323like files, but instead of containing data directly like files do they
2424contain other files and directories.
2525
26- #### Paths
26+ ### Paths
2727
2828Directories and files have a path, like ` C:\Users\me\hello.py ` . That just
2929means 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
178178` readline() ` method that reads the next line, and returns ` '' `
179179if we're at the end of the file.
180180
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+ ```
182191
183192There's only one confusing thing about reading files. If you try
184193to 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:
199208>> >
200209```
201210
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.
203215
204216``` py
205217>> > first = []
@@ -247,7 +259,38 @@ use the `read()` method.
247259>> >
248260```
249261
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+ ```
251294
252295## Example: File viewer
253296
0 commit comments