summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorVille Skyttä <ville.skytta@iki.fi>2016-07-26 16:09:03 +0300
committerVille Skyttä <ville.skytta@iki.fi>2016-07-26 16:21:12 +0300
commit41259f3ae6bbf0c2fc1ff87a1b2c403d80057a06 (patch)
tree62adb3f7a36a16e2dc488e4b729d38f70dd47929 /docs
parent36d778fbeca8d6dccbef6b1e5f25e0c4fa676360 (diff)
downloadurllib3-41259f3ae6bbf0c2fc1ff87a1b2c403d80057a06.tar.gz
Use "with" to close more files eagerly and also on error
Diffstat (limited to 'docs')
-rw-r--r--docs/user-guide.rst6
1 files changed, 4 insertions, 2 deletions
diff --git a/docs/user-guide.rst b/docs/user-guide.rst
index ec74bb1c..b37343aa 100644
--- a/docs/user-guide.rst
+++ b/docs/user-guide.rst
@@ -162,7 +162,8 @@ For uploading files using ``multipart/form-data`` encoding you can use the same
approach as :ref:`form_data` and specify the file field as a tuple of
``(file_name, file_data)``::
- >>> file_data = open('example.txt').read()
+ >>> with open('example.txt') as fp:
+ ... file_data = fp.read()
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',
@@ -186,7 +187,8 @@ to specify the file's MIME type explicitly::
For sending raw binary data simply specify the ``body`` argument. It's also
recommended to set the ``Content-Type`` header::
- >>> binary_data = open('example.jpg', 'rb').read()
+ >>> with open('example.jpg', 'rb') as fp:
+ ... binary_data = fp.read()
>>> r = http.request(
... 'POST',
... 'http://httpbin.org/post',