{"id":8128,"date":"2023-11-07T18:27:24","date_gmt":"2023-11-07T12:57:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=8128"},"modified":"2023-11-10T12:22:09","modified_gmt":"2023-11-10T06:52:09","slug":"python-data-presistence-os-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-presistence-os-module\/","title":{"rendered":"Python Data Presistence – os Module"},"content":{"rendered":"

Python Data Presistence – os Module<\/h2>\n

This is another module containing useful utilities for manipulating files and directories programmatically. The usual operating system commands for directory management have equivalents in this module. There is mkdir ( ) function that creates a new directory inside the current directory by default. To create it at any other location in the file system, its absolute path has to be given.<\/p>\n

Example<\/strong><\/p>\n\n\n\n
import os
\nos.mkdir(1newdir’) # inside current directory
\nos.mkdir(‘c:\\\\newdir’) #in C drive<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

The chdir ( ) function (to set current working directory) and rmdir ( ) function (to remove a directory) also take relative path by default. A ge tcwd ( ) function is also available to know which is the current working directory. All these operations are demonstrated in following interpreter session:<\/p>\n

Example<\/strong><\/p>\n\n\n\n
>> import os
\n>>> os.mkdir(“newdir”) #new directory in current path
\n>>> os.chdir(“newdir”) #set current directory
\n>>> os.getcwdO #displays current working directory
\n‘E:\\\\python37\\\\newdir’
\n>>> os.rmdir(“newdir”) #shouldn’t be current directory and should be empty Traceback (most recent call last):
\nFile “<stdin>”, line 1, in <module> FileNotFoundError: [WinError 2] The system cannot
\nfind the file specified: ‘newdir’
\n>>> os.chdir) #sets current directory to parent
\ndirectory
\n>>> os.getcwd ( )
\n‘ E:\\\\python3 7 ‘
\n>>> os.rmdir(“newdir”) #now it can be deleted
\n>>> os.listdirO #returns list of files and directories in current path
\n[‘abcdemo.py’, ‘aifftest.py’, ‘checkindent.py’,
\n‘clbr.py’, 1combinations-2.py’, ‘combinations.py’,
\n‘comprehensionl.py’, ‘conditionals.jpg’, ‘continue- example.py’, ‘data_class.py’, ‘DLLs\u2019, ‘Doc’, ‘else- in-loop.py’, ‘ equitriangle .py’ , ‘fibolist.py’ ,
\n‘ findertest.py’, ‘for-l.py’, ‘for-2.py’, ‘for-3.
\npy’, ‘for-4.py’, ‘for-5.py’, ‘for-6.py’, ‘for-7.
\npy’, ‘gcexample.py’, ‘hello.py’, ‘include’, ‘Lib’,
\n‘libs’, ‘LICENSE.txt’, ‘modfinder.py’, ‘modspec. py’, ‘modulel.py’, ‘module2.py’, ‘mydb.sqlite3’,
\n‘myfunctions.cover’, ‘myfunctions.py’, ‘mymain.
\ncover’, ‘mymain.py’, ‘nestedfor.py’, ‘newdirbak’,
\n‘NEWS.txt’, ‘out.aiff’, ‘out.au’, ‘out.mp3’,
\n‘out.wma’, ‘polar.png’, ‘python.exe’, ‘python3. dll’, ‘python37.dll’, ‘pythonw.exe’, ‘report.txt’,
\n‘runpyexample.py’, ‘sample.wav’, ‘Scripts’, ‘secret- number.py’, ‘securepwd.py’, ‘sound.aiff’, ‘sound,
\nwav’, ‘sqrmodule.py’, ‘structexample.py’, ‘tabdemo.py’, ‘taxl.py’, ‘tax2.py’, ‘tax3.py'( ‘tax4. py’, ‘tel’, ‘testindent.py’, ‘Tools’, ‘triangle, py’, ‘trianglebrowser.py’, ‘vcruntimel40.dll’,
\n‘warningexample.py’, ‘wavetest.py’, ‘while-1.py’, ‘_threadexample.py’, ‘ pycache ‘]
\n>>><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

The os module also has functions to create a new file and perform read\/ write operations on it. We shall learn about these functions in the chapter on File Handling.<\/p>\n","protected":false},"excerpt":{"rendered":"

Python Data Presistence – os Module This is another module containing useful utilities for manipulating files and directories programmatically. The usual operating system commands for directory management have equivalents in this module. There is mkdir ( ) function that creates a new directory inside the current directory by default. To create it at any other …<\/p>\n

Python Data Presistence – 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 Presistence - 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-presistence-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 Presistence - os Module - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Presistence – os Module This is another module containing useful utilities for manipulating files and directories programmatically. The usual operating system commands for directory management have equivalents in this module. There is mkdir ( ) function that creates a new directory inside the current directory by default. To create it at any other … Python Data Presistence – os Module Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-presistence-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-07T12:57:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:52:09+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-presistence-os-module\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/\",\"name\":\"Python Data Presistence - os Module - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-07T12:57:24+00:00\",\"dateModified\":\"2023-11-10T06:52:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-presistence-os-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Presistence – os Module\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Presistence – os Module\",\"datePublished\":\"2023-11-07T12:57:24+00:00\",\"dateModified\":\"2023-11-10T06:52:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-presistence-os-module\/#webpage\"},\"wordCount\":438,\"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-presistence-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 Presistence - 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-presistence-os-module\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Presistence - os Module - Python Programs","og_description":"Python Data Presistence – os Module This is another module containing useful utilities for manipulating files and directories programmatically. The usual operating system commands for directory management have equivalents in this module. There is mkdir ( ) function that creates a new directory inside the current directory by default. To create it at any other … Python Data Presistence – os Module Read More »","og_url":"https:\/\/python-programs.com\/python-data-presistence-os-module\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-07T12:57:24+00:00","article_modified_time":"2023-11-10T06:52:09+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-presistence-os-module\/#webpage","url":"https:\/\/python-programs.com\/python-data-presistence-os-module\/","name":"Python Data Presistence - os Module - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-07T12:57:24+00:00","dateModified":"2023-11-10T06:52:09+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-presistence-os-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-presistence-os-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-presistence-os-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Presistence – os Module"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-presistence-os-module\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-presistence-os-module\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Presistence – os Module","datePublished":"2023-11-07T12:57:24+00:00","dateModified":"2023-11-10T06:52:09+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-presistence-os-module\/#webpage"},"wordCount":438,"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-presistence-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\/8128"}],"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=8128"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8128\/revisions"}],"predecessor-version":[{"id":8130,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8128\/revisions\/8130"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}