3838```
3939
4040` C:\Users\me\hello.py ` is an ** absolute path** . But there are also
41- ** relative paths** . For example, if you're in ` C:\Users ` , ` me\hello.py `
41+ ** relative paths** . For example, if we are in ` C:\Users ` , ` me\hello.py `
4242is same as ` C:\Users\me\hello.py ` . The place we are in is sometimes
4343called ** current directory** , ** working directory** or
4444** current working directory** .
@@ -67,7 +67,7 @@ Let's create a file and write a hello world to it.
6767```
6868
6969Doesn't seem like it did anything. But actually it created a ` hello.txt `
70- somewhere on your system. On Windows it's probably in ` C:\Users\YourName ` ,
70+ somewhere on our system. On Windows it's probably in ` C:\Users\YourName ` ,
7171and on most other systems it should be in ` /home/yourname ` . You can open
7272it with notepad or any other plain text editor your system comes with.
7373
@@ -88,7 +88,7 @@ the current working directory.
8888
8989The second argument was ` w ` ... but where the heck does that come from?
9090` w ` is short for write, and that just means that we'll create a new file.
91- There's some other modes you can use also:
91+ There's some other modes we can use also:
9292
9393| Mode | Short for | Meaning |
9494| -------| -----------| -----------------------------------------------------------------------|
@@ -100,7 +100,7 @@ The `w` and `a` modes create a new file if it exists already, but trying
100100to read from a non-existent file is an error.
101101
102102But what is that ` with ourfile as f ` crap? That's just a fancy way to make
103- sure that the file gets closed, no matter what happens. As you can see,
103+ sure that the file gets closed, no matter what happens. As we can see,
104104the file was indeed closed.
105105
106106``` py
@@ -115,7 +115,7 @@ to the file we opened using `file=f`.
115115
116116## Reading from files
117117
118- After opening a file with the ` r ` mode you can for loop over it, just
118+ After opening a file with the ` r ` mode we can for loop over it, just
119119like it was a list. So let's go ahead and read everything in the file
120120we created to a list of lines.
121121
@@ -130,7 +130,23 @@ we created to a list of lines.
130130>> >
131131```
132132
133- But why is there a ` \n ` at the end of our hello world?
133+ Trying to open a non-existent file with ` w ` created the file for us, but
134+ doing that with ` r ` gives us an error instead. We'll learn more about
135+ errors later.
136+
137+ ``` py
138+ >> > with open (' this-doesnt-exist.txt' , ' r' ) as f:
139+ ... print (" It's working!" )
140+ ...
141+ Traceback (most recent call last):
142+ File " <stdin>" , line 1 , in < module>
143+ FileNotFoundError : [Errno 2 ] No such file or directory: ' this-doesnt-exist.txt'
144+ >> >
145+ ```
146+
147+ So now we have the hello world in the ` lines ` variable, but it's
148+ ` ['Hello World!\n'] ` instead of ` ['Hello World!'] ` . So what the heck did
149+ that ` \n ` doing there?
134150
135151` \n ` means newline. Note that it needs to be a backslash, so ` /n `
136152doesn't have any special meaning like ` \n ` has. When we wrote the file
@@ -160,9 +176,9 @@ There we go, each of our lines now ends with a `\n`. When we for
160176loop over the file it's divided into lines based on where the ` \n `
161177characters are, not based on how we printed to it.
162178
163- But how to get rid of that ` \n ` ? The ` rstrip `
164- [ string method] ( handy-stuff-strings.md#string-methods ) is great
165- for this:
179+ But how to get rid of that ` \n ` ? [ The rstrip string
180+ method] ( https://docs.python.org/3/library/stdtypes.html#str.rstrip ) is
181+ great for this:
166182
167183``` py
168184>> > stripped = []
@@ -174,9 +190,9 @@ for this:
174190>> >
175191```
176192
177- It's also possible to read lines one by one. Files have a
178- ` readline() ` method that reads the next line, and returns ` '' `
179- if we're at the end of the file.
193+ It's also possible to read lines one by one. Files have [ a readline
194+ method ] ( https://docs.python.org/3/library/io.html#io.TextIOBase.readline )
195+ that reads the next line, and returns ` '' ` if we're at the end of the file.
180196
181197``` py
182198>> > with open (' hello.txt' , ' r' ) as f:
@@ -189,8 +205,8 @@ if we're at the end of the file.
189205' Hello two!\n '
190206```
191207
192- There's only one confusing thing about reading files. If you try
193- to read it twice you 'll find out that it only gets read once:
208+ There's only one confusing thing about reading files. If we try
209+ to read it twice we 'll find out that it only gets read once:
194210
195211``` py
196212>> > first = []
@@ -232,11 +248,12 @@ again and everything works.
232248```
233249
234250Usually it's best to just read the file once, and use the
235- content you have read from it multiple times.
251+ content we have read from it multiple times.
236252
237- As you can see, files behave a lot like lists. The ` join() `
238- string method joins together strings from a list, but we can
239- also use it to join together lines of a file:
253+ As we can see, files behave a lot like lists. [ The join string
254+ method] ( https://docs.python.org/3/library/stdtypes.html#str.join ) joins
255+ together strings from a list, but we can also use it to join together
256+ lines of a file:
240257
241258``` py
242259>> > with open (' hello.txt' , ' r' ) as f:
@@ -247,8 +264,8 @@ also use it to join together lines of a file:
247264>> >
248265```
249266
250- But if you need all of the content as a string, you can just
251- use the ` read() ` method .
267+ But if we need all of the content as a string, we can just use [ the read
268+ method ] ( https://docs.python.org/3/library/io.html#io.TextIOBase.read ) .
252269
253270``` py
254271>> > with open (' hello.txt' , ' r' ) as f:
@@ -259,7 +276,7 @@ use the `read()` method.
259276>> >
260277```
261278
262- You can also open full paths, like ` open('C:\\Users\\me\\myfile.txt', 'r') ` .
279+ We can also open full paths, like ` open('C:\\Users\\me\\myfile.txt', 'r') ` .
263280The reason why we need to use ` \\ ` when we really mean ` \ ` is that
264281backslash has a special meaning. There are special characters like
265282` \n ` , and ` \\ ` means an actual backslash.
285302>> >
286303```
287304
288- If you don't use Windows and your paths don't contain backslashes
289- you don't need to double anything or use ` r ` in front of paths.
305+ If our program is not meant to be ran on Windows and the paths
306+ don't contain backslashes we don't need to double anything or use
307+ ` r ` in front of paths.
290308
291309``` py
292310>> > print (' /some/name' )
0 commit comments