@@ -69,7 +69,9 @@ Let's create a file and write a hello world to it.
6969Doesn't seem like it did anything. But actually it created a ` hello.txt `
7070somewhere 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
72- it with notepad or any other plain text editor your system comes with.
72+ it with notepad or any other plain text editor your system comes with by
73+ opening the folder that contains the file and then double-clicking the
74+ file.
7375
7476So how does that code work?
7577
@@ -82,23 +84,22 @@ object that is assigned to the variable `f`.
8284>> >
8385```
8486
85- So the first argument we passed to ` open ` was the path we wanted to write.
87+ File objects are not the same thing as paths and filenames, so if we try
88+ to use ` 'hello.txt' ` like we used ` f ` it doesn't work.
89+
90+ The first argument we passed to ` open ` was the path we wanted to write.
8691Our path was more like a filename than a path, so the file ended up in
8792the current working directory.
8893
89- The second argument was ` w ` ... but where the heck does that come from?
90- ` w ` is short for write, and that just means that we'll create a new file.
91- There's some other modes we can use also:
94+ The second argument was ` w ` . It's short for write, and that just means
95+ that we'll create a new file. There's some other modes we can use also:
9296
9397| Mode | Short for | Meaning |
9498| -------| -----------| -----------------------------------------------------------------------|
9599| ` r ` | read | Read from an existing file. |
96100| ` w ` | write | Write to a file. ** If the file exists, its old content is removed.** |
97101| ` a ` | append | Write to the end of a file, and keep the old content. |
98102
99- The ` w ` and ` a ` modes create a new file if it exists already, but trying
100- to read from a non-existent file is an error.
101-
102103But what is that ` with ourfile as f ` crap? That's just a fancy way to make
103104sure that the file gets closed, no matter what happens. As we can see,
104105the file was indeed closed.
@@ -130,9 +131,9 @@ we created to a list of lines.
130131>> >
131132```
132133
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] ( exceptions.md ) .
134+ Trying to open a non-existent file with ` w ` or ` a ` creates the file for
135+ us, but doing that with ` r ` gives us an error instead. We'll learn more
136+ about errors [ later] ( exceptions.md ) .
136137
137138``` py
138139>> > with open (' this-doesnt-exist.txt' , ' r' ) as f:
@@ -150,11 +151,11 @@ that `\n` doing there?
150151
151152` \n ` means newline. Note that it needs to be a backslash, so ` /n `
152153doesn't have any special meaning like ` \n ` has. When we wrote the file
153- with print it actually added a ` \n ` to the end of it. It's good practise
154+ with print it actually added a ` \n ` to the end of it. It's recommended
154155to end the content of files with a newline character, but it's not
155156necessary.
156157
157- So how does that work if we have more than one line in the file?
158+ Let's see how that works if we have more than one line in the file.
158159
159160``` py
160161>> > with open (' hello.txt' , ' w' ) as f:
@@ -206,7 +207,8 @@ that reads the next line, and returns `''` if we're at the end of the file.
206207```
207208
208209There'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:
210+ to read the same file object twice we'll find out that it only gets read
211+ once:
210212
211213``` py
212214>> > first = []
@@ -226,8 +228,8 @@ to read it twice we'll find out that it only gets read once:
226228
227229File objects remember their position. When we tried to read the
228230file again it was already at the end, and there was nothing left
229- to read. But if we open the file again, it's in the beginning
230- again and everything works.
231+ to read. But if we open the file again, we get a new file object that
232+ is in the beginning and everything works.
231233
232234``` py
233235>> > first = []
@@ -250,21 +252,7 @@ again and everything works.
250252Usually it's best to just read the file once, and use the
251253content we have read from it multiple times.
252254
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:
257-
258- ``` py
259- >> > with open (' hello.txt' , ' r' ) as f:
260- ... full_content = ' ' .join(f)
261- ...
262- >> > full_content
263- ' Hello one!\n Hello two!\n Hello three!\n '
264- >> >
265- ```
266-
267- But if we need all of the content as a string, we can just use [ the read
255+ If we need all of the content as a string, we can use [ the read
268256method] ( https://docs.python.org/3/library/io.html#io.TextIOBase.read ) .
269257
270258``` py
@@ -297,8 +285,8 @@ adding an `r` to the beginning of the string. In this case the `r`
297285is short for "raw", not "read".
298286
299287``` py
300- >> > r ' C:\s ome\n ame' == ' C: \\ some \\ name '
301- True
288+ >> > r ' C:\s ome\n ame'
289+ ' C: \\ some \\ name '
302290>> >
303291```
304292
@@ -312,6 +300,11 @@ don't contain backslashes we don't need to double anything or use
312300>> >
313301```
314302
303+ Doing things like ` open('C:\\Users\\me\\myfile.txt', 'r') ` is not
304+ recommended because the code needs to be modified if someone wants to
305+ run the program on a different computer that doesn't have a
306+ ` C:\Users\me ` folder.
307+
315308## Examples
316309
317310This program prints the contents of files:
0 commit comments