diff options
| author | Ville Skyttä <ville.skytta@iki.fi> | 2016-07-26 16:09:03 +0300 |
|---|---|---|
| committer | Ville Skyttä <ville.skytta@iki.fi> | 2016-07-26 16:21:12 +0300 |
| commit | 41259f3ae6bbf0c2fc1ff87a1b2c403d80057a06 (patch) | |
| tree | 62adb3f7a36a16e2dc488e4b729d38f70dd47929 /docs | |
| parent | 36d778fbeca8d6dccbef6b1e5f25e0c4fa676360 (diff) | |
| download | urllib3-41259f3ae6bbf0c2fc1ff87a1b2c403d80057a06.tar.gz | |
Use "with" to close more files eagerly and also on error
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/user-guide.rst | 6 |
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', |
