{"id":4377,"date":"2023-11-23T12:13:16","date_gmt":"2023-11-23T12:13:16","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4377"},"modified":"2025-05-01T16:30:41","modified_gmt":"2025-05-01T15:30:41","slug":"write-file","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/file-input-and-output\/write-file","title":{"rendered":"Write File"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Write text file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>response = requests.get(\"https:\/\/somedomain.com\/somepage\", timeout=10)    #Timeout in secs, Get something you want to store\nfile1 = open(\"response.txt\", \"w\")\nfile1.write(response.text)\nfile1.close()\n\n\/\/Or, using with (handles close for you whatever happens):\nwith open(\"response.txt\", \"w\") as file1:\n\tfile1.write(response.text)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Write binary file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># Request the profile picture of the OP:\nresponse = requests.get(\"https:\/\/somedomain.com\/someimage.jpg\", timeout=10)    #Timeout in secs, Get a file you want to store\nfile1 = open(\"response.jpg\", \"wb\")\nfile1.write(response.content)\nfile1.close()\n\n\/\/Or, using with (handles close for you whatever happens)\nwith open(\"response.jpg\", \"wb\") as file1:\n\tfile1.write(response.content)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write text file Write binary file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344],"tags":[],"class_list":["post-4377","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4377"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4377\/revisions"}],"predecessor-version":[{"id":4927,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4377\/revisions\/4927"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}