1- Open function
1+ `` open `` Function
22-------------
33
44`open <http://docs.python.org/dev/library/functions.html#open >`__ opens
@@ -17,18 +17,18 @@ you spot them all? If not, read on. By the end of this article, you'll
1717know what's wrong in the above code, and, more importantly, be able to
1818avoid these mistakes in your own code. Let's start with the basics:
1919
20- The return of open is a file handle, given out from the operating system
20+ The return value from `` open `` is a file handle, given out from the operating system
2121to your Python application. You will want to return this file handle
2222once you're finished with the file, if only so that your application
23- won't reach the limit of the number of open file handle it can have at
23+ won't reach the limit of the number of open file handles it can have at
2424once.
2525
2626Explicitly calling ``close `` closes the file handle, but only if the
2727read was successful. If there is any error just after ``f = open(...) ``,
2828``f.close() `` will not be called (depending on the Python interpreter,
2929the file handle may still be returned, but that's another story). To
3030make sure that the file gets closed whether an exception occurs or not,
31- pack it into a ``` with `` statement:
31+ pack it into a ``with `` statement:
3232
3333.. code :: python
3434
@@ -53,28 +53,28 @@ or text mode (a string of characters).
5353
5454In general, if the format is written by humans, it tends to be text
5555mode. ``jpg `` image files are not generally written by humans (and are
56- indeed not readable to humans), and you should therefore open them in
57- binary mode by adding a ``b `` to the text string (if you're following
56+ indeed not readable by humans), and you should therefore open them in
57+ binary mode by adding a ``b `` to the mode string (if you're following
5858the opening example, the correct mode would be ``rb ``). If you open
5959something in text mode (i.e. add a ``t ``, or nothing apart from
60- ``r/r+/w/a ``), you must also know which encoding to use - for a
60+ ``r/r+/w/a ``), you must also know which encoding to use. For a
6161computer, all files are just bytes, not characters.
6262
6363Unfortunately, ``open `` does not allow explicit encoding specification
6464in Python 2.x. However, the function
6565`io.open <http://docs.python.org/2/library/io.html#io.open >`__ is
6666available in both Python 2.x and 3.x (where it is an alias of ``open ``),
6767and does the right thing. You can pass in the encoding with the
68- ``encoding `` keyword. If you don't pass in any encoding, a system- ( and
69- Python-) specific default will be picked. You may be tempted to rely on
68+ ``encoding `` keyword. If you don't pass in any encoding, a system -- and
69+ Python -- specific default will be picked. You may be tempted to rely on
7070these defaults, but the defaults are often wrong, or the default
71- encoding cannot actually express all characters (this will happen on
71+ encoding cannot actually express all characters in the file (this will happen often on
7272Python 2.x and/or Windows). So go ahead and pick an encoding. ``utf-8 ``
7373is a terrific one. When you write a file, you can just pick the encoding
7474to your liking (or the liking of the program that will eventually read
7575your file).
7676
77- How do you find out which encoding a file you read has ? Well,
77+ How do you find out which encoding a file you're reading was written in ? Well,
7878unfortunately, there is no foolproof way to detect the encoding - the
7979same bytes can represent different, but equally valid characters in
8080different encodings. Therefore, you must rely on metadata (for example,
@@ -93,11 +93,11 @@ determines whether it's JPG (hint: These files start with the bytes
9393 jpgdata = inf.read()
9494
9595 if jpgdata.startswith(b ' \xff\xd8 ' ):
96- text = u ' This is a jpeg file (%d bytes long)\n '
96+ text = u ' This is a JPEG file (%d bytes long)\n '
9797 else :
9898 text = u ' This is a random file (%d bytes long)\n '
9999
100100 with io.open(' summary.txt' , ' w' , encoding = ' utf-8' ) as outf:
101101 outf.write(text % len (jpgdata))
102102
103- I am sure that now you would use ``open `` correctly!
103+ I am sure that now you will use ``open `` correctly!
0 commit comments