diff --git a/files.html b/files.html index 7c2a0d9..864bb27 100644 --- a/files.html +++ b/files.html @@ -28,13 +28,14 @@ body{counter-reset:h1 12}
a_file = open('examples/chinese.txt', encoding='utf-8')
-Python has a built-in open() function, which takes a filename as an argument. Here the filename is 'examples/chinese.txt'. There are four interesting things about this filename:
+
Python has a built-in open() function, which takes a filename as an argument. Here the filename is 'examples/chinese.txt'. There are five interesting things about this filename:
open() function only takes one. In Python, whenever you need a “filename,” you can include some or all of a directory path as well.
But that call to the open() function didn’t stop at the filename. There’s another argument, called encoding. Oh dear, that sounds dreadfully familiar.
@@ -92,47 +93,62 @@ UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 28: chara
>>> a_file = open('examples/chinese.txt', encoding='utf-8')
->>> a_file.read()
-'Dive Into Python 是为有经验的程序员编写的一本 Python 书。\n'
+>>> a_file.read() ①
+'Dive Into Python 是为有经验的程序员编写的一本 Python 书。\n'
+>>> a_file.read() ②
+''
read() method. The result is a string.
+FIXME +
What if you want to re-read a file?
->>> a_file.seek(0) +# continued from the previous example +>>> a_file.read() ① +'' +>>> a_file.seek(0) ② 0 ->>> a_file.read(16) +>>> a_file.read(16) ③ 'Dive Into Python' ->>> a_file.read(1) +>>> a_file.read(1) ④ ' ' ->>> a_file.read(1) +>>> a_file.read(1) ⑤ '是' ->>> a_file.tell() +>>> a_file.tell() ⑥ 20
FIXME
->>> a_file.seek(17) +# continued from the previous example +>>> a_file.seek(17) ① 17 ->>> a_file.read(1) +>>> a_file.read(1) ② '是' ->>> a_file.tell() +>>> a_file.tell() ③ 20
FIXME
->>> a_file.seek(18) ->>> a_file.read(1) +>>> a_file.seek(18) ① +18 +>>> a_file.read(1) ② Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> a_file.read(1) @@ -141,45 +157,48 @@ UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 28: chara UnicodeDecodeError: 'utf8' codec can't decode byte 0x98 in position 0: unexpected code byte
Open files consume system resources, and depending on the file mode, other programs may not be able to access them. It’s important to close files as soon as you’re finished with them. - +
+# continued from the previous example +>>> a_file.close()-
FIXME checking if a file is closed +
Well that was anticlimactic. + +
The file object a_file still exists; calling its close() method doesn’t destroy the object itself. But it’s not terribly useful.
+
+
+# continued from the previous example +>>> a_file.read() ① +Traceback (most recent call last): + File "<pyshell#24>", line 1, in <module> + a_file.read() +ValueError: I/O operation on closed file. +>>> a_file.seek(0) ② +Traceback (most recent call last): + File "<pyshell#25>", line 1, in <module> + a_file.seek(0) +ValueError: I/O operation on closed file. +>>> a_file.tell() ③ +Traceback (most recent call last): + File "<pyshell#26>", line 1, in <module> + a_file.tell() +ValueError: I/O operation on closed file. +>>> a_file.close() ④ +>>> a_file.closed ⑤ +True+
IOError exception.
+tell() method also fails.
+close() method on a file object whose file has been closed does not raise an exception. It’s just a no-op.
+closed attribute will confirm that the file is closed.
+with Statement