{"id":925,"date":"2021-06-04T22:44:47","date_gmt":"2021-06-04T17:14:47","guid":{"rendered":"https:\/\/cbsepython.in\/?p=925"},"modified":"2021-06-05T23:22:55","modified_gmt":"2021-06-05T17:52:55","slug":"operations-on-text-file","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/operations-on-text-file\/","title":{"rendered":"Operations on text file"},"content":{"rendered":"<h2>Python Data File Handling<\/h2>\n<h3>operations on text file<\/h3>\n<p>A text file consists of a sequence of lines. A line is a sequence of characters, stored on permanent storage. In a text file, each line is terminated by a special character, known as End Of Line (EOL). Text file can be created using any text editor. Ex. Myfile.txt.<\/p>\n<p>&nbsp;<\/p>\n<p>Opening a text file: Open ()<\/p>\n<p>When we want to read or write a file, we must have to open is first. Open () function takes the name of a file as the first argument. The second argument indicates the mode of accessing the file.<\/p>\n<p>Syntax:<\/p>\n<p>&lt;file variable&gt;=open(file name, access mode)<\/p>\n<p>Modes for opening a file:<\/p>\n<p>read(r) : to read the file<\/p>\n<p>write(w): to write the file<\/p>\n<p>append(a):\u00a0 to write at the end of file<\/p>\n<p>&nbsp;<\/p>\n<p>Ex:<\/p>\n<p>&gt;&gt;&gt; f=open(&#8216;myfile.txt&#8217;)<\/p>\n<p>&gt;&gt;&gt; print(f)<\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>&lt;_io.TextIOWrapper name='myfile.txt' mode='r' encoding='cp1252'&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Closing file: close()<\/p>\n<p>Syntax:\u00a0 file_Object.close()<\/p>\n<p>Example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">f=open('myfile.txt')\r\nprint(\"The file which is to be open using the given command is:\",f.name)\r\nf.close()\r\n<\/pre>\n<p>Output:<\/p>\n<pre>The file which is to be open using the given command is: myfile.txt<\/pre>\n<p>&nbsp;<\/p>\n<p>Properties of File Object:<\/p>\n<p>&nbsp;<\/p>\n<p>name: shows the file name of opened file<\/p>\n<p>mode: shows Mode in which the file gets opened<\/p>\n<p>readable: returns Boolean value, which indicates whether the file is readable or not<\/p>\n<p>closed: returns Boolean value, which indicates whether the file is closed or not<\/p>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">f=open('myfile.txt', 'w')\r\nprint(\"Name of File:\",f.name)\r\nprint(\"Mode of File:\",f.mode)\r\nprint(\"File readable :\",f.readable())\r\nprint(\"File closed:\",f.closed)\r\nf.close()\r\nprint(\"File closed:\",f.closed, f.name)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>Name of File: myfile.txt\r\nMode of File: w\r\nFile readable : False\r\nFile closed: False\r\nFile closed: True myfile.txt<\/pre>\n<p>&nbsp;<\/p>\n<p>Reading form a file:<\/p>\n<p>We can read character data from text file by using the following read methods:<\/p>\n<p>&nbsp;<\/p>\n<p>read(): To read the entire data from the file; starts reading from the cursor up to the end of the file.<\/p>\n<p>Synatx:<\/p>\n<p>file_Object.read()<\/p>\n<p>Example:<\/p>\n<p>The text file named myfile.txt is already stored in my local disk with the data shown in image.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-937\" src=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/06\/Text_File-300x115.jpg\" alt=\"CBSE file handling\" width=\"605\" height=\"232\" title=\"\" srcset=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/06\/Text_File-300x115.jpg 300w, https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/06\/Text_File-400x153.jpg 400w, https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/06\/Text_File.jpg 407w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">f=open('myfile.txt', 'r')\r\ndata=f.read()\r\nprint(data)\r\nf.close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>== RESTART: C:\/Users\/ATC\/AppData\/Local\/Programs\/Python\/Python38-32\/filehandling.py =\r\nHello,\r\nWelcome to the CBSE class 12 students\r\nHaving computer science with python\r\nYou are learning Python file handling\r\n&gt;&gt;&gt;\r\n\r\n<\/pre>\n<p>read(n): To read &#8216;n&#8217; characters from the file, starting from the cursor.<\/p>\n<p>Example: Here we will read only first 21 characters from the file.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">f=open('myfile.txt', 'r')\r\ndata=f.read(21)\r\nprint(data)\r\nf.close()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>Hello,\r\nWelcome to the<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>readline(): To read only one line from the file; starts reading from the cursor up to, and including, the end of line character.<\/p>\n<p>readlines(): To read all lines from the file into a list; starts reading from the cursor up to the end of the file and returns a list of lines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Data File Handling operations on text file A text file consists of a sequence of lines. A line is a sequence of characters, stored on permanent storage. In a text file, each line is terminated by a special character, known as End Of Line (EOL). Text file can be created using any text editor. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-925","post","type-post","status-publish","format-standard","hentry","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}