{"id":8350,"date":"2023-11-09T18:42:31","date_gmt":"2023-11-09T13:12:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=8350"},"modified":"2023-11-10T12:24:42","modified_gmt":"2023-11-10T06:54:42","slug":"python-data-persistence-file-handling-using-os-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/","title":{"rendered":"Python Data Persistence – File Handling using os Module"},"content":{"rendered":"

Python Data Persistence – File Handling using os Module<\/h2>\n

Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them.<\/p>\n

The open ()<\/strong> function from the os module (obviously it needs to be referred to as os. open ( ))<\/strong> is similar to the built-in open ( )<\/strong> function in the sense it also opens a file for reading\/write operations. However, it doesn\u2019t return a file or file-like object but a file descriptor, an integer corresponding to the file opened. File descriptor\u2019s values 0, 1, and 2 are reserved for stdin,<\/strong> stout<\/strong>, and stder r<\/strong> streams. Other files will be given an incremental file descriptor. Also, the write ( )<\/strong> and read( )<\/strong> functions of the os module needs bytes to object as the argument. The os .open ()<\/strong> function needs to be provided one or combinations of the following constants: (Table 5.2)<\/p>\n\n\n\n\n\n\n\n\n
os.OWRONLY<\/td>\nopen for writing only<\/td>\n<\/tr>\n
os.ORDWR<\/td>\nopen for reading and writing<\/td>\n<\/tr>\n
os.OAPPEND<\/td>\nappend on each write<\/td>\n<\/tr>\n
os.OCREAT<\/td>\ncreate file if it does not exist<\/td>\n<\/tr>\n
os.OTRUNC<\/td>\ntruncate size to 0<\/td>\n<\/tr>\n
os.OEXCL<\/td>\nerror if create and file exists<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

As in the case of a file object, the os module defines a sleek ()<\/strong> function to set file r\/w position at the desired place from the beginning, current position, or end indicated by integers 0,1, and 2 respectively.<\/p>\n

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

>>> fd=os . open (\"testfile. txt\", os .0_RDWR | os .0_CREAT)\r\n>>> text=\"Hello Python\"\r\n>>> encoded=text.encode(encoding='utf-16')\r\n>>> os.write(fd, encoded)\r\n>>> os.lseek(fd,0,0)\r\n>>> encoded=os.read(fd)\r\n>>> os.path.getsizeCtestfile.txt\") #calculate file size\r\n>>> encoded=os.read(fd, 26)\r\n>>> text=encoded.decode('utf-16 ')\r\n>>> text\r\n'Hello Python'<\/pre>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

Python Data Persistence – File Handling using os Module Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them. The open () function from the os module (obviously it needs to be referred …<\/p>\n

Python Data Persistence – File Handling using os Module<\/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 - File Handling using os Module - 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-file-handling-using-os-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Persistence - File Handling using os Module - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Persistence – File Handling using os Module Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them. The open () function from the os module (obviously it needs to be referred … Python Data Persistence – File Handling using os Module Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/\" \/>\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:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:54:42+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-file-handling-using-os-module\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/\",\"name\":\"Python Data Persistence - File Handling using os Module - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-09T13:12:31+00:00\",\"dateModified\":\"2023-11-10T06:54:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Persistence – File Handling using os Module\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Persistence – File Handling using os Module\",\"datePublished\":\"2023-11-09T13:12:31+00:00\",\"dateModified\":\"2023-11-10T06:54:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#webpage\"},\"wordCount\":245,\"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-file-handling-using-os-module\/#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 - File Handling using os Module - 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-file-handling-using-os-module\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Persistence - File Handling using os Module - Python Programs","og_description":"Python Data Persistence – File Handling using os Module Python\u2019s built-in library has an os module that provides useful operating system-dependent functions. It also provides functions for performing low-level read\/write operations on the file. Let us briefly get acquainted with them. The open () function from the os module (obviously it needs to be referred … Python Data Persistence – File Handling using os Module Read More »","og_url":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-09T13:12:31+00:00","article_modified_time":"2023-11-10T06:54:42+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-file-handling-using-os-module\/#webpage","url":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/","name":"Python Data Persistence - File Handling using os Module - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-09T13:12:31+00:00","dateModified":"2023-11-10T06:54:42+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Persistence – File Handling using os Module"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Persistence – File Handling using os Module","datePublished":"2023-11-09T13:12:31+00:00","dateModified":"2023-11-10T06:54:42+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-file-handling-using-os-module\/#webpage"},"wordCount":245,"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-file-handling-using-os-module\/#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\/8350"}],"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=8350"}],"version-history":[{"count":3,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8350\/revisions"}],"predecessor-version":[{"id":9410,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8350\/revisions\/9410"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}