

{"id":6860,"date":"2018-01-31T11:05:02","date_gmt":"2018-01-31T05:35:02","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=6860"},"modified":"2026-04-20T16:00:36","modified_gmt":"2026-04-20T10:30:36","slug":"python-directory","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-directory\/","title":{"rendered":"Python Directory &amp; File Management &#8211; A Quick and Easy Tutorial"},"content":{"rendered":"<p>So far, we\u2019ve mostly only seen the computational capabilities of Python. Today, we\u2019ll talk about how we can use it to handle Python directory.<\/p>\n<p>After this tutorial, you\u2019ll be able to create, rename, and list files in a directory in Python, and work with the Python Directory.<\/p>\n<p>In a computer system, files are organized into directories. These may contain subdirectories and files. Indeed, this makes a vital part of a user-friendly UI.<\/p>\n<p>But don\u2019t be confused; a dictionary is simply what you call a folder.<\/p>\n<p>In this Directories in Python tutorial, we will import the OS module to be able to access the methods we will apply.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os<\/pre>\n<div id=\"attachment_6862\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6862\" class=\"wp-image-6862 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg\" alt=\"Python Directory\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-6862\" class=\"wp-caption-text\">Introduction to Python Directory<\/p><\/div>\n<h3>How to Get the Current Python Directory?<\/h3>\n<p>Python makes working with folders easy. The os and pathlib modules let you find where you are, create new folders, and list everything inside them. Using simple commands like os.getcwd() and pathlib.Path.cwd() shows the current directory at once. This clear view helps beginners stay organized while they code.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.getcwd()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&#8216;C:\\\\Users\\\\lifei\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python36-32&#8217;<\/p>\n<\/div>\n<p>Cwd is for current working directory in Python. This returns the path of the current Python directory as a string in Python.<\/p>\n<p>To get it as a bytes object, we use the method getcwdb().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.getcwdb()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>b&#8217;C:\\\\Users\\\\lifei\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python36-32&#8242;<\/p>\n<\/div>\n<p>Here, we get two backslashes instead of one. This is because the first one is to escape the second one since this is a string object.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(os.getcwd())\r\n&lt;class 'str'&gt;<\/pre>\n<p>To render it properly, use the Python method with the print statement.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; print(os.getcwd())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32<\/p>\n<\/div>\n<h3>Changing Current Python Directory<\/h3>\n<p>To change our current working directories in Python, we use the chdir() method.<\/p>\n<p>This takes one argument- the path to the directory to which to change.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('C:\\Users\\lifei')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>SyntaxError: (unicode error)<\/p>\n<\/div>\n<p>&#8216;unicodeescape&#8217; code can&#8217;t decode bytes in position 2-3: truncated \\UXXXXXXXX escape<\/p>\n<p>But remember that when using backward slashes, it is recommended to escape the backward slashes to avoid a problem.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei')\r\n&gt;&gt;&gt; os.getcwd()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&#8216;C:\\\\Users\\\\lifei&#8217;<\/p>\n<\/div>\n<p>When you restart the shell, we get back to the default working Python directory.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei')\r\n&gt;&gt;&gt; os.getcwd()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&#8216;C:\\\\Users\\\\lifei&#8217;<\/p>\n<\/div>\n<p>You can also use forward slashes for the path. This way, you don\u2019t have to use backward slashes to escape.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('C:\/Users\/lifei')\r\n&gt;&gt;&gt; os.getcwd()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&#8216;C:\\\\Users\\\\lifei&#8217;<\/p>\n<\/div>\n<p>Finally, you can also use double quotes.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir(\"C:\\\\Users\\\\lifei\")<\/pre>\n<h3>Python List Directories and Files<\/h3>\n<p><strong>We can use three methods to get the files in the directory:<\/strong><\/p>\n<ul>\n<li><strong>os.listdir() method:<\/strong> This method gives you the list of all the files and folders in the directory.<\/li>\n<li><strong>os.walk() method:<\/strong> It generates the file names in the directory tree.<\/li>\n<li><strong>os.scandir() method:<\/strong> It is a faster and more efficient way to list files and folders as compared to os.listdir().<\/li>\n<\/ul>\n<p>To get the contents of a directory into a Python list, we use the listdir() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;.atom&#8217;, &#8216;.eclipse&#8217;, &#8216;.idlerc&#8217;, &#8216;.p2&#8217;, &#8216;.tooling&#8217;, &#8216;.vscode&#8217;, &#8216;3D Objects&#8217;, &#8216;afiedt.buf&#8217;, &#8216;AppData&#8217;, &#8216;Application Data&#8217;, &#8216;Contacts&#8217;, &#8216;Cookies&#8217;, &#8216;Desktop&#8217;, &#8216;Documents&#8217;, &#8216;Downloads&#8217;, &#8216;Dropbox&#8217;, &#8216;eclipse&#8217;, &#8216;eclipse-workspace&#8217;, &#8216;eclipse-workspace-C++&#8217;, &#8216;eclipse-workspace-EE&#8217;, &#8216;Favorites&#8217;, &#8216;iCloudDrive&#8217;, &#8216;IntelGraphicsProfiles&#8217;, &#8216;Links&#8217;, &#8216;Local Settings&#8217;, &#8216;MicrosoftEdgeBackups&#8217;, &#8216;Music&#8217;, &#8216;My Documents&#8217;, &#8216;NetHood&#8217;, &#8216;NTUSER.DAT&#8217;, &#8216;ntuser.dat.LOG1&#8217;, &#8216;ntuser.dat.LOG2&#8217;, &#8216;NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TM.blf&#8217;, &#8216;NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TMContainer00000000000000000001.regtrans-ms&#8217;, &#8216;NTUSER.DAT{03a9cc49-f0a2-11e7-904c-d9989e93b548}.TMContainer00000000000000000002.regtrans-ms&#8217;, &#8216;ntuser.ini&#8217;, &#8216;OneDrive&#8217;, &#8216;Oracle&#8217;, &#8216;Pictures&#8217;, &#8216;PrintHood&#8217;, &#8216;Recent&#8217;, &#8216;Roaming&#8217;, &#8216;Saved Games&#8217;, &#8216;Searches&#8217;, &#8216;SendTo&#8217;, &#8216;Start Menu&#8217;, &#8216;Templates&#8217;, &#8216;Videos&#8217;, &#8216;workspace&#8217;]<\/p>\n<\/div>\n<p>Note that this includes the hidden and system files as well.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir(\"C:\\\\Users\\\\lifei\\\\Desktop\")\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;Adobe Photoshop CS2.lnk&#8217;, &#8216;Atom.lnk&#8217;, &#8216;Burn Book.txt&#8217;, &#8216;desktop.ini&#8217;, &#8216;Documents&#8217;, &#8216;Eclipse Cpp Oxygen.lnk&#8217;, &#8216;Eclipse Java Oxygen.lnk&#8217;, &#8216;Eclipse Jee Oxygen.lnk&#8217;, &#8216;For the book.txt&#8217;, &#8216;Items for trip.txt&#8217;, &#8216;Papers&#8217;, &#8216;Remember to remember.txt&#8217;, &#8216;Sweet anticipation.png&#8217;, &#8216;Today.txt&#8217;, &#8216;topics.txt&#8217;, &#8216;unnamed.jpg&#8217;]<\/p>\n<\/div>\n<p>This shows us the contents on the desktop. This was about the Python List directory.<\/p>\n<h3>How to Create a Python Directory?<\/h3>\n<p>We can also create new Python directories with the mkdir() method. It takes one argument, that is, the path of the new python directory to create.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.mkdir('Christmas Photos')\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;Adobe Photoshop CS2.lnk&#8217;, &#8216;Atom.lnk&#8217;, &#8216;Burn Book.txt&#8217;, &#8216;Christmas Photos&#8217;, &#8216;desktop.ini&#8217;, &#8216;Documents&#8217;, &#8216;Eclipse Cpp Oxygen.lnk&#8217;, &#8216;Eclipse Java Oxygen.lnk&#8217;, &#8216;Eclipse Jee Oxygen.lnk&#8217;, &#8216;For the book.txt&#8217;, &#8216;Items for trip.txt&#8217;, &#8216;Papers&#8217;, &#8216;Remember to remember.txt&#8217;, &#8216;Sweet anticipation.png&#8217;, &#8216;Today.txt&#8217;, &#8216;topics.txt&#8217;, &#8216;unnamed.jpg&#8217;]<\/p>\n<\/div>\n<p>Here, we supplied the name of the python directory to create.<\/p>\n<p>We can also create a directory in a directory other than the current working directory in python. For this, you must specify the full path.<\/p>\n<h3>How to Rename Python Directory?<\/h3>\n<p>To rename directories in python, we use the rename() method. It takes two arguments- the python directory to rename, and the new name for it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.rename('Christmas Photos','Christmas 2017')\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;Adobe Photoshop CS2.lnk&#8217;, &#8216;Atom.lnk&#8217;, &#8216;Burn Book.txt&#8217;, &#8216;Christmas 2017&#8217;, &#8216;desktop.ini&#8217;, &#8216;Documents&#8217;, &#8216;Eclipse Cpp Oxygen.lnk&#8217;, &#8216;Eclipse Java Oxygen.lnk&#8217;, &#8216;Eclipse Jee Oxygen.lnk&#8217;, &#8216;For the book.txt&#8217;, &#8216;Items for trip.txt&#8217;, &#8216;Papers&#8217;, &#8216;Remember to remember.txt&#8217;, &#8216;Sweet anticipation.png&#8217;, &#8216;Today.txt&#8217;, &#8216;topics.txt&#8217;, &#8216;unnamed.jpg&#8217;]<\/p>\n<\/div>\n<h3>How to Remove Python Directory\/File?<\/h3>\n<p>We made a file named \u2018Readme.txt\u2019 inside our folder Christmas 2017. To delete this file, we use the method remove().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop\\\\Christmas 2017')\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;Readme.txt&#8217;]<\/p>\n<\/div>\n<p>To remove a Python directory, we use the rmdir() method. But for this, the directory must be empty.<\/p>\n<p>So we\u2019ll add Readme.txt again to check if we\u2019re able to delete Christmas 2017.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.rmdir('Christmas 2017')\r\nTraceback (most recent call last):\r\nFile \"&lt;pyshell#412&gt;\", line 1, in &lt;module&gt;\r\nos.rmdir('Christmas 2017')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>OSError: [WinError 145] The directory is not empty: &#8216;Christmas 2017&#8217;<\/p>\n<\/div>\n<p>As you can see, it raised a Python exception called OSError.<\/p>\n<p>So let\u2019s first remove the file and then delete the Python directory.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.remove('C:\\\\Users\\\\lifei\\\\Desktop\\\\Christmas 2017\\\\Readme.txt')\r\n&gt;&gt;&gt; os.rmdir('Christmas 2017')\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;Adobe Photoshop CS2.lnk&#8217;, &#8216;Atom.lnk&#8217;, &#8216;Burn Book.txt&#8217;, &#8216;desktop.ini&#8217;, &#8216;Documents&#8217;, &#8216;Eclipse Cpp Oxygen.lnk&#8217;, &#8216;Eclipse Java Oxygen.lnk&#8217;, &#8216;Eclipse Jee Oxygen.lnk&#8217;, &#8216;For the book.txt&#8217;, &#8216;Items for trip.txt&#8217;, &#8216;Papers&#8217;, &#8216;Remember to remember.txt&#8217;, &#8216;Sweet anticipation.png&#8217;, &#8216;Today.txt&#8217;, &#8216;topics.txt&#8217;, &#8216;unnamed.jpg&#8217;]<\/p>\n<\/div>\n<h3>Joining and Splitting Path<\/h3>\n<p>We must use platform-independent file and directory paths in Python, so our program runs on every platform. We use the submodule os.path for this.<\/p>\n<p>join() in python joins path components and returns a path as a string. It adds appropriate separators (\\ for Windows and \/ for Unix)<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.join('C:','Users','lifei','Desktop')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&#8216;C:Users\\\\lifei\\\\Desktop&#8217;<\/p>\n<\/div>\n<p>Conversely, split() splits the path into components, removing the separator.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.split('C:Users\\\\lifei\\\\Desktop')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>(&#8216;C:Users\\\\lifei&#8217;, &#8216;Desktop&#8217;)<\/p>\n<\/div>\n<h3>Checking if Python Directory Exists<\/h3>\n<p>It is possible to check whether a path exists. We use the exists() function for this. Also, this is in the os.path submodule.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.exists('C:\\\\Users\\\\lifei\\\\Desktop')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>True<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.exists('C:\\\\Users\\\\lifei\\\\Desktop\\\\Myfolder')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>False<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.exists('C:\\\\Users\\\\lifei\\\\Desktop\\\\topics.txt')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>True<\/p>\n<\/div>\n<p>Then, to check whether that path leads us to a directory, we use the isdir() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.isdir('C:\\\\Users\\\\lifei\\\\Desktop')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>True<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.path.isdir('C:\\\\Users\\\\lifei\\\\Desktop\\\\topics.txt')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>False<\/p>\n<\/div>\n<h3>Recursively Traversing a Directory in Python<\/h3>\n<p>The walk() function lets us recursively traverse a directory. This means that it returns the roots, subdirectories, and files in a directory.<\/p>\n<p>You can traverse it using for loops in Python.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for roots,dirs,files in os.walk('C:\\\\Users\\\\lifei\\\\Desktop\\\\Papers'):\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(roots,len(dirs),len(files))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">C:\\Users\\lifei\\Desktop\\Papers 1 29<br \/>\nC:\\Users\\lifei\\Desktop\\Papers\\Newfolder 0 1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt; for roots,dirs,files in os.walk('C:\\\\Users\\\\lifei\\\\Desktop\\\\Papers'): \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n                    print(roots,dirs,files)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>C:\\Users\\lifei\\Desktop\\Papers [&#8216;Newfolder&#8217;] [&#8216;cs 15jun.pdf&#8217;, &#8216;sc 11jun.pdf&#8217;, &#8216;sc 12dec.pdf&#8217;, &#8216;sc 12jun.pdf&#8217;, &#8216;sc 13jun.pdf&#8217;, &#8216;sc 14jun.pdf&#8217;, &#8216;sc 15jun.pdf&#8217;, &#8216;sc 16dec.pdf&#8217;, &#8216;sc 16jun.pdf&#8217;, &#8216;sc 17jun.pdf&#8217;, &#8216;Syllabus.pdf&#8217;, &#8216;we 10jun.pdf&#8217;, &#8216;we 11jun.pdf&#8217;, &#8216;we 12jun.pdf&#8217;, &#8216;we 13jun.pdf&#8217;, &#8216;we 14jun.pdf&#8217;, &#8216;we 15jun.pdf&#8217;, &#8216;we 16dec.pdf&#8217;, &#8216;we 16jun.pdf&#8217;, &#8216;we 17jun.pdf&#8217;, &#8216;wn 10jun.pdf&#8217;, &#8216;wn 11jun.pdf&#8217;, &#8216;wn 13jun.pdf&#8217;, &#8216;wn 14jun.pdf&#8217;, &#8216;wn 15jun.pdf&#8217;, &#8216;wn 16dec.pdf&#8217;, &#8216;wn 16jun.pdf&#8217;, &#8216;wn 17jun.pdf&#8217;, &#8216;wn jun12.pdf&#8217;]C:\\Users\\lifei\\Desktop\\Papers\\Newfolder [] [&#8216;readme.txt&#8217;]<\/p>\n<\/div>\n<p>Actually, these give us Python generator objects. This is why we can traverse on them.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.walk('C:\\\\Users\\\\lifei\\\\Desktop\\\\Papers')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;generator object walk at 0x05E0CE10&gt;<\/div>\n<h3>Python Interview Questions on Directories<\/h3>\n<p>1. How to create a new directory in Python?<\/p>\n<p>2. How to get a list of directories in Python?<\/p>\n<p>3. How to compare two directories in Python?<\/p>\n<p>4. How to check if directory exists in Python?<\/p>\n<p>5. How to create a parent directory in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>What we discussed today are just a few methods that Python provides you to work with files and directories.<\/p>\n<p>With these, we can create a Python directory, rename it, and delete them. You can also traverse them and check if a path exists.<\/p>\n<p>Feel powerful yet? There\u2019s more to come on our journey with Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far, we\u2019ve mostly only seen the computational capabilities of Python. Today, we\u2019ll talk about how we can use it to handle Python directory. After this tutorial, you\u2019ll be able to create, rename, and&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":6862,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[3116,5093,10644,11527,14904,16276],"class_list":["post-6860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-creating-a-python-directory","tag-getting-current-python-directory","tag-python-list-directories-and-files","tag-renaming-a-python-directory","tag-traverse-a-directory","tag-working-with-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Directory &amp; File Management - A Quick and Easy Tutorial - DataFlair<\/title>\n<meta name=\"description\" content=\"Python Directory &amp; files management- Create, Rename, list, Remove &amp; Change Python Directories, joining and Splitting Paths, Recursively Traversing Directory\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-directory\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Directory &amp; File Management - A Quick and Easy Tutorial - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python Directory &amp; files management- Create, Rename, list, Remove &amp; Change Python Directories, joining and Splitting Paths, Recursively Traversing Directory\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-directory\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-31T05:35:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T10:30:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Directory &amp; File Management - A Quick and Easy Tutorial - DataFlair","description":"Python Directory & files management- Create, Rename, list, Remove & Change Python Directories, joining and Splitting Paths, Recursively Traversing Directory","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:\/\/data-flair.training\/blogs\/python-directory\/","og_locale":"en_US","og_type":"article","og_title":"Python Directory &amp; File Management - A Quick and Easy Tutorial - DataFlair","og_description":"Python Directory & files management- Create, Rename, list, Remove & Change Python Directories, joining and Splitting Paths, Recursively Traversing Directory","og_url":"https:\/\/data-flair.training\/blogs\/python-directory\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-31T05:35:02+00:00","article_modified_time":"2026-04-20T10:30:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Directory &amp; File Management &#8211; A Quick and Easy Tutorial","datePublished":"2018-01-31T05:35:02+00:00","dateModified":"2026-04-20T10:30:36+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/"},"wordCount":1413,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg","keywords":["Creating a Python Directory","Getting Current Python Directory","Python List Directories and Files","Renaming a Python Directory","Traverse a directory","working with python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-directory\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-directory\/","url":"https:\/\/data-flair.training\/blogs\/python-directory\/","name":"Python Directory &amp; File Management - A Quick and Easy Tutorial - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg","datePublished":"2018-01-31T05:35:02+00:00","dateModified":"2026-04-20T10:30:36+00:00","description":"Python Directory & files management- Create, Rename, list, Remove & Change Python Directories, joining and Splitting Paths, Recursively Traversing Directory","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-directory\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Directories-in-Python.jpg","width":1200,"height":628,"caption":"Python Directory"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-directory\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Directory &amp; File Management &#8211; A Quick and Easy Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=6860"}],"version-history":[{"count":14,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6860\/revisions"}],"predecessor-version":[{"id":147716,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/6860\/revisions\/147716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/6862"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=6860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=6860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=6860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}