{"id":723,"date":"2022-02-24T13:56:33","date_gmt":"2022-02-24T18:56:33","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=723"},"modified":"2023-11-29T14:55:47","modified_gmt":"2023-11-29T19:55:47","slug":"python-file-management","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-file-management\/","title":{"rendered":"Python File Management"},"content":{"rendered":"\n<p>This page shows how to work with text files using Python3 code. Following is a sample txt file we will be using. Lets called it stocks.txt<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bce.to\nhnd.to \nmtl.to\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading from File<\/h3>\n\n\n\n<p>You can use read(), readline(), or readlines() function to read from a file. This example uses read()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stocks = open(\"data\/stocks.txt\",\"r\")\nstocks.read()\nstocks.close()\n<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'bce.to\\nhnd.to\\nmtl.to'\n<\/code><\/pre>\n\n\n\n<p><strong>Using readline():<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stocks = open(\"data\/stocks.txt\",\"r\")\nstocks.readline()\nstocks.readline()\nstocks.close()\n<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bce.to\n\nhnd.to\n<\/code><\/pre>\n\n\n\n<p>Note that each readline() will print the next row in the file.<\/p>\n\n\n\n<p><strong>Using readlines():<\/strong>&nbsp;Note the plural readlines as opposed to readline.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stocks = open(\"data\/stocks.txt\",\"r\")\nstocks.readlines()\nstocks.close()\n<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'bce.to\\n', 'hnd.to\\n', 'mtl.to']\n<\/code><\/pre>\n\n\n\n<p>readlines() outputs each row as a list element<\/p>\n\n\n\n<p><strong>Easy way to remove newlines<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stocks = open(\"data\/stocks.txt\",\"r\")\nfor stocksymbol in stocks.read().splitlines():\n    print(stocksymbol)\nstocks.close()\n<\/code><\/pre>\n\n\n\n<p>output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bce.to\nhnd.to\nmtl.to\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Write to File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fh = open(\"data\/stocks.txt\", \"w\")\nnewstock = 'ta.to'\nfh.write(newstock)\nfh.close()\n<\/code><\/pre>\n\n\n\n<p>After you run the code, stocks.txt will contain<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ta.to\n<\/code><\/pre>\n\n\n\n<p>Your code overwrote the existing contents of the file. To append the text rather than overwriting, use the &#8220;a&#8221; option:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fh = open(\"data\/stocks.txt\", \"w\")\nnewstock = 'ta.to'\nfh.write(newstock)\nfh.close()\n<\/code><\/pre>\n\n\n\n<p>After you run the code, stocks.txt will contain<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bce.to\nhnd.to\nmtl.to\nta.to<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This page shows how to work with text files using Python3 code. Following is a sample txt file we will be using. Lets called it stocks.txt Reading from File You can use read(), readline(), or readlines() function to read from a file. This example uses read() output: Using readline(): output: Note that each readline() will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1238,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[209,137],"class_list":["post-723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-file-management","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=723"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/723\/revisions\/724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1238"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}