Skip to content

Commit 2c0317f

Browse files
committed
files and modules
1 parent a4f88a8 commit 2c0317f

File tree

4 files changed

+544
-42
lines changed

4 files changed

+544
-42
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ it.**
3434
9. [Handy stuff with strings](handy-stuff-strings.md)
3535
10. [Loops](loops.md)
3636
11. [Files](files.md)
37+
12. [Modules](modules.md)
38+
13. [Defining functions](defining-functions.md)
3739

3840
Parts of this tutorial that aren't ready to be added to the rest of it
3941
yet:
4042

41-
- [Defining functions](defining-functions.md)
4243
- [Classes](classes.md)
4344

4445
Other things this tutorial comes with:
@@ -47,6 +48,7 @@ Other things this tutorial comes with:
4748
- [Contact me](contact-me.md)
4849
- [Setting up a text editor](editor-setup.md)
4950
- [Answers for the exercises](answers.md)
51+
- [The TODO list](TODO.md)
5052

5153
I'm Akuli and I have written most of this tutorial, but the following
5254
people have helped me with it:

TODO.md

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,19 @@ This tutorial is not complete. It still needs:
66
are needed for dict keys)
77
- Explain `for a, b in thing` somewhere
88
- **More exercises and examples everywhere!**
9-
- reading and writing files
10-
- first of all: what are files, directories/folders and paths
11-
- print(..., file=thefile), not thefile.write(...)
12-
- show how to iterate over the file and .read() it
13-
- why .rstrip('\n') instead of .strip()
14-
- files remember their position, reading the same file object twice
15-
- modules
16-
- just files and folders, have a look at Python's lib directory
17-
- "batteries included" filosofy
18-
- briefly introduce some of the most commonly used standard library
19-
modules:
20-
- math
21-
- it's not a file, what is it?
22-
- sys
23-
- sys.exit() vs exit()
24-
- time
25-
- os, os.path, subprocess
26-
- random
27-
- webbrowser
28-
- explain even more briefly and link to documentation/tutorials:
29-
- configparser
30-
- textwrap
31-
- collections, itertools, functools
32-
- traceback
33-
- types
34-
- warnings
35-
- json
36-
- link to third party modules:
37-
- appdirs
38-
- requests
399
- Exceptions
4010
- explain bool(x) and why it matters
4111
- classes part 2
4212
- non-public `_variables`
4313
- singular inheritance, inheritance of built-in classes
4414
- using super
15+
- no classes part 3 with multiple inheritance, it's not something people need
4516
- quick overview of some of the most useful built-in functions and classes
4617
- range
4718
- zip
4819
- classmethod and staticmethod
49-
- iterables and iterators: something most Python programmers need to be aware of
50-
- classes part 3: multiple inheritance
51-
- an advanced thing
52-
- built-in classes don't use it
53-
- diamond diagrams, why to use super()
20+
- iterables and iterators: something most Python programmers need to be
21+
aware of
5422
- last chapter: "What should I do now?" links to other resources
5523
- GUI programming tutorials
5624
- easygui
@@ -60,3 +28,9 @@ This tutorial is not complete. It still needs:
6028
- a pygame tutorial
6129
- David Beazley's metaprogramming and other talks
6230
- a regex tutorial
31+
32+
33+
## Fix these
34+
35+
- the chapter about installing python should mention that python 2 and 3
36+
can be and often are installed at the same time

files.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
These are simple thing that many computer users already know, but I'll go
66
through 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

2222
Directories are a way to group files. They also have a name and a location
2323
like files, but instead of containing data directly like files do they
2424
contain other files and directories.
2525

26-
#### Paths
26+
### Paths
2727

2828
Directories and files have a path, like `C:\Users\me\hello.py`. That just
2929
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
178178
`readline()` method that reads the next line, and returns `''`
179179
if 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

183192
There's only one confusing thing about reading files. If you try
184193
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:
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\name')
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:\some\name' == '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

Comments
 (0)