{"id":8348,"date":"2023-11-09T18:42:28","date_gmt":"2023-11-09T13:12:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=8348"},"modified":"2023-11-10T12:24:52","modified_gmt":"2023-11-10T06:54:52","slug":"python-data-persistence-simultaneous-read-write","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/","title":{"rendered":"Python Data Persistence – Simultaneous Read\/Write"},"content":{"rendered":"

Python Data Persistence – Simultaneous Read\/Write<\/h2>\n

Modes \u2018w\u2019 or \u2018a\u2019 allow a file to be written to, but not to be read from. Similarly, \u2018r\u2019 mode facilitates reading a file but prohibits writing data to it. To be able to perform both read\/write operations on a file without closing it, add the \u2018+\u2019 sign to these mode characters. As a result \u2018w+\u2019, \u2018r+\u2019<\/strong> or \u2018a+\u2019<\/strong> mode will open the file for simultaneous read\/write. Similarly, binary read\/ write simultaneous operations are enabled on file if opened with \u2018wb+\u2019, \u2018rb+\u2019<\/strong> or \u2018ab+\u2019<\/strong> modes.<\/p>\n

It is also possible to perform read or write operations at any byte position of the file. As you go on writing data in a file, the end of file (EOF)<\/strong> position keeps on moving away from its beginning. By default, new data is written at the current EOF position. When opened in \u2018r\u2019 mode, reading starts from the 0th byte i.e. from the beginning of the file.
\nThe seek ( )<\/strong> method of file object lets you set current reading or writing position to a desired byte position in the file. It locates the desired position by counting the offset distance from beginning (0), current position (1), or EOF (2). Following example illustrates this point:<\/p>\n

Example<\/strong><\/p>\n

>>> file=open (\"testfile . txt\", \"w+\")\r\n>>> file .write (\"This is a rat race\")\r\n>>> file.seek(10,0) #seek 10th byte from beginning\r\n>>> txt=file . read (3) #read next 3 bytes\r\n>>> txt\r\n' rat'\r\n>>> file . seek (10,0) #seek 10th byte position\r\n>>> file . write ('cat' ) #overwrite next 3 bytes\r\n>>> file.seek(0)\r\n>>> text=file . read () #read entire file\r\n>>> text\r\n'This is a cat race'\r\n>>> file . close ( )<\/pre>\n

Of course, this may not work correctly if you try to insert new data as it may overwrite part of existing data. One solution could be to read the entire content in a memory variable, modify it, and rewrite it after truncating the existing file. Other built-in modules like file input and map allow modifying files in place. However, later in this book, we are going to discuss a more sophisticated tool for manipulating databases and update data on a random basis.<\/p>\n","protected":false},"excerpt":{"rendered":"

Python Data Persistence – Simultaneous Read\/Write Modes \u2018w\u2019 or \u2018a\u2019 allow a file to be written to, but not to be read from. Similarly, \u2018r\u2019 mode facilitates reading a file but prohibits writing data to it. To be able to perform both read\/write operations on a file without closing it, add the \u2018+\u2019 sign to …<\/p>\n

Python Data Persistence – Simultaneous Read\/Write<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Data Persistence - Simultaneous Read\/Write - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Persistence - Simultaneous Read\/Write - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Persistence – Simultaneous Read\/Write Modes \u2018w\u2019 or \u2018a\u2019 allow a file to be written to, but not to be read from. Similarly, \u2018r\u2019 mode facilitates reading a file but prohibits writing data to it. To be able to perform both read\/write operations on a file without closing it, add the \u2018+\u2019 sign to … Python Data Persistence – Simultaneous Read\/Write Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-09T13:12:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:54:52+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/\",\"name\":\"Python Data Persistence - Simultaneous Read\/Write - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-09T13:12:28+00:00\",\"dateModified\":\"2023-11-10T06:54:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Persistence – Simultaneous Read\/Write\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Persistence – Simultaneous Read\/Write\",\"datePublished\":\"2023-11-09T13:12:28+00:00\",\"dateModified\":\"2023-11-10T06:54:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage\"},\"wordCount\":295,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Data Persistence - Simultaneous Read\/Write - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Persistence - Simultaneous Read\/Write - Python Programs","og_description":"Python Data Persistence – Simultaneous Read\/Write Modes \u2018w\u2019 or \u2018a\u2019 allow a file to be written to, but not to be read from. Similarly, \u2018r\u2019 mode facilitates reading a file but prohibits writing data to it. To be able to perform both read\/write operations on a file without closing it, add the \u2018+\u2019 sign to … Python Data Persistence – Simultaneous Read\/Write Read More »","og_url":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-09T13:12:28+00:00","article_modified_time":"2023-11-10T06:54:52+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage","url":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/","name":"Python Data Persistence - Simultaneous Read\/Write - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-09T13:12:28+00:00","dateModified":"2023-11-10T06:54:52+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Persistence – Simultaneous Read\/Write"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Persistence – Simultaneous Read\/Write","datePublished":"2023-11-09T13:12:28+00:00","dateModified":"2023-11-10T06:54:52+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#webpage"},"wordCount":295,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-data-persistence-simultaneous-read-write\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8348"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=8348"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8348\/revisions"}],"predecessor-version":[{"id":9409,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8348\/revisions\/9409"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}