diff --git a/files.html b/files.html index 2390083..79da47c 100644 --- a/files.html +++ b/files.html @@ -24,12 +24,20 @@ body{counter-reset:h1 12}
FIXME +
Before you can read from a file, you need to open it. Opening a file in Python couldn’t be easier: -
-open(..., encoding='...') -open(..., 'r', encoding='...') -+
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:
+
+
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.
Python has a built-in function, open(), for opening a file on disk. The open() function returns a file object, which has methods and attributes for getting information about and manipulating the file.
+
+>>> a_file = open('examples/chinese.txt', encoding='utf-8')
+>>> a_file.name
+'examples/chinese.txt'
+>>> a_file.mode
+'r'
+>>> a_file.encoding
+'utf-8'
+