{"id":1560,"date":"2018-11-13T21:07:06","date_gmt":"2018-11-13T20:07:06","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1560"},"modified":"2018-11-13T21:07:39","modified_gmt":"2018-11-13T20:07:39","slug":"create-a-file-at-ease","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/create-a-file-at-ease\/","title":{"rendered":"Create a file at ease"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/i.stack.imgur.com\/hOlgI.png\" alt=\"Image result for python open file\" \/><\/p>\n<p>One of the most annoying thing it to me to write this code<\/p>\n<pre class=\"lang:default decode:true\">import os\r\nwith open(filename, \"w\", encoding=\"utf-8\") as file:\r\n    file.write(content)\r\n# This is to open the file created\r\nos.system(filename)<\/pre>\n<p>Just to create a new file and write the content in it. So I decided to create a module just for that called createfile.<\/p>\n<pre class=\"lang:default decode:true\">import os\r\n\r\ndef creafile(filename, content):\r\n\t\"Create a file\"\r\n\ttry:\r\n\t\twith open(filename, \"w\", encoding=\"utf-8\") as file:\r\n\t\t\tfile.write(content)\r\n\t\tos.system(filename)\r\n\texcept:\r\n\t\tprint(\"You must use an argument for the filename ('prova.html') and another for the content ('&lt;b&gt;Hello&lt;\/b&gt; World')\")\r\n\r\nif __name__ == \"__main__\":\r\n\tcreafile(\"filediprova.html\", \"&lt;b&gt;Hello&lt;\/b&gt;World\")<\/pre>\n<p>So now, when I want to create a file with something in it and see immediately, I just write this code<\/p>\n<pre class=\"lang:default decode:true \">from createfile import createfile\r\n\r\ncreatefile(\"myfile.html\", \"&lt;b&gt;Hello&lt;\/b&gt; World\")<\/pre>\n<p>So, with one line, I create the file, save it with the content and open it to see it. I like it, what do you think. Is it worthwhile, or it is better to write everytime alle that boring code?<\/p>\nhttps:\/\/pythonprogramming.altervista.org\/how-to-create-a-list-of-tuple-from-a-string-pythonically\/\r\n\r\n<script>\r\n\tvar title = \"Basic Stuffs in Python\"\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/open-read-write-and-append-content-files-with-python\/\", \"Read & write files\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/how-to-create-a-list-of-tuple-from-a-string-pythonically\/\",\"Write tuple easily\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/le-basi-luso-delle-liste-1\/\",\"Liste 1 (in italiano)\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/join-two-or-more-lists\/\",\"How to join lists in Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-a-file-at-ease\/\",\"Create files easily\"]\r\n\t\t];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\r\n\n","protected":false},"excerpt":{"rendered":"A shortcut to create and open file with less code in python, creating your own module. Save time and stress, making life easier.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/create-a-file-at-ease\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":1512,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[11,266],"tags":[106,318,32,4,319],"class_list":["post-1560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modules","category-tutorial","tag-code","tag-create-file","tag-modules","tag-python","tag-save-file"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=1560"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1560\/revisions"}],"predecessor-version":[{"id":1562,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1560\/revisions\/1562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/1512"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}