Skip to content

Commit f6681c1

Browse files
Edits to open Function.
1 parent 5321a78 commit f6681c1

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

open_function.rst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
1717
know what's wrong in the above code, and, more importantly, be able to
1818
avoid 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
2121
to your Python application. You will want to return this file handle
2222
once 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
2424
once.
2525

2626
Explicitly calling ``close`` closes the file handle, but only if the
2727
read was successful. If there is any error just after ``f = open(...)``,
2828
``f.close()`` will not be called (depending on the Python interpreter,
2929
the file handle may still be returned, but that's another story). To
3030
make 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

5454
In general, if the format is written by humans, it tends to be text
5555
mode. ``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
5858
the opening example, the correct mode would be ``rb``). If you open
5959
something 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
6161
computer, all files are just bytes, not characters.
6262

6363
Unfortunately, ``open`` does not allow explicit encoding specification
6464
in Python 2.x. However, the function
6565
`io.open <http://docs.python.org/2/library/io.html#io.open>`__ is
6666
available in both Python 2.x and 3.x (where it is an alias of ``open``),
6767
and 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
7070
these 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
7272
Python 2.x and/or Windows). So go ahead and pick an encoding. ``utf-8``
7373
is a terrific one. When you write a file, you can just pick the encoding
7474
to your liking (or the liking of the program that will eventually read
7575
your 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,
7878
unfortunately, there is no foolproof way to detect the encoding - the
7979
same bytes can represent different, but equally valid characters in
8080
different 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

Comments
 (0)