{"id":2358,"date":"2020-01-07T15:20:26","date_gmt":"2020-01-07T15:20:26","guid":{"rendered":"https:\/\/www.askpython.com\/?p=2358"},"modified":"2023-02-16T19:57:19","modified_gmt":"2023-02-16T19:57:19","slug":"python-directory-operations","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-directory-operations","title":{"rendered":"Python Directory Operations"},"content":{"rendered":"\n<p><code><strong>Directory<\/strong><\/code> is basically a structure that contains all the corresponding documents, files, and folders.<\/p>\n\n\n\n<p>Python&#8217;s <a href=\"https:\/\/www.askpython.com\/python-modules\/python-os-module-10-must-know-functions\" class=\"rank-math-link\">os module<\/a> contains multiple functions for directory management.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python directories functions<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"\"><tbody><tr><td><strong><em>os.access(path, mode)<\/em><\/strong><br>It uses the <code>uid<\/code> to check for the path&#8217;s access.<\/td><\/tr><tr><td><strong><em>os.chdir(path)<\/em><\/strong><br>It changes the CWD to the path specified by the user.<\/td><\/tr><tr><td><strong><em>os.chflags(path, flags)<\/em><\/strong><br>It is used to set the flags to the numeric flags.<\/td><\/tr><tr><td><strong><em>os.chmod(path, mode)<\/em><\/strong><br>It is used to change the path&#8217;s mode to the numeric mode.<\/td><\/tr><tr><td><strong><em>os.chown(path, uid, gid)<\/em><\/strong><br>It changes the group id and owner of <em>path<\/em> to the numeric <code>uid<\/code> and <code>gid<\/code>. <\/td><\/tr><tr><td><strong><em>os.chroot(path)<\/em><\/strong><br>It is used to change the root directory of the currently executing process to the path specified by the user.<\/td><\/tr><tr><td><strong><em>os.fchdir(fd)<\/em><\/strong><br>It is used to change the CWD to the directory represented in the file descriptor <code>fd<\/code>. <\/td><\/tr><tr><td><strong><em>os.getcwd()<\/em><\/strong><br>It returns the current working directory (CWD).<\/td><\/tr><tr><td><strong><em>os.getcwdu()<\/em><\/strong><br>It returns Unicode object as output, represented by the CWD.<\/td><\/tr><tr><td><strong><em>os.lchmod(path, mode)<\/em><\/strong><br>It is used to change the path&#8217;s mode to the numeric mode.<\/td><\/tr><tr><td><strong><em>os.listdir(path)<\/em><\/strong><br>Returns a list containing the names of the entries in the directory given by path.<\/td><\/tr><tr><td><strong><em>os.lstat(path)<\/em><\/strong><br><\/td><\/tr><tr><td><strong><em>os.makedirs(path[, mode])<\/em><\/strong><br>It is used to create the directories in a recursive manner.<\/td><\/tr><tr><td><strong><em>os.mkdir( )<\/em><\/strong><br>It is used to creates a new directory with a  named path.<\/td><\/tr><tr><td><strong><em>os.mkfifo(path[, mode])<\/em><\/strong><br><\/td><\/tr><tr><td><strong><em>os.readlink(path)<\/em><\/strong><br><\/td><\/tr><tr><td><strong><em>os.removedirs(path)<\/em><\/strong><br>It is used to remove the corresponding directories recursively.<\/td><\/tr><tr><td><strong><em>os.rename(src, dst)<\/em><\/strong><br>It is used to rename the src directory to the dst.<\/td><\/tr><tr><td><strong><em>os.renames(old, new)<\/em><\/strong><br>It is used to rename the old directories with a new one in a recursive manner.<\/td><\/tr><tr><td><strong><em>os.rmdir(path)<\/em><\/strong><br>It is used to remove the directory specified by the user. <\/td><\/tr><tr><td><strong><em>os.stat(path)<\/em><\/strong><br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creating of a New Directory<\/h3>\n\n\n\n<p>Python&#8217;s os module provides <code><strong>mkdir()<\/strong><\/code> function to create a new directory.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>os.mkdir('name')<\/code><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.mkdir(&#039;C:\/python1&#039;) #create a direcotry abc in C:\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"956\" height=\"365\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/create-directory.png\" alt=\"Create Directory\" class=\"wp-image-2359\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/create-directory.png 956w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/create-directory-300x115.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/01\/create-directory-768x293.png 768w\" sizes=\"auto, (max-width: 956px) 100vw, 956px\" \/><figcaption>Create Directory<\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Get the Current Working Directory<\/h3>\n\n\n\n<p>The <code><strong>getcwd()<\/strong><\/code> function is used to get the location of the CWD.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.getcwd() \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wdir='C:\/Users\/HP\/PycharmProjects\/Hello'<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Rename a Directory<\/h3>\n\n\n\n<p>The <code><strong>rename()<\/strong><\/code> function is used to rename the CWD.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>os.rename(old,new)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Fetch the List of Files\/Directories in a Directory<\/h3>\n\n\n\n<p>The <code><strong>listdir()<\/strong><\/code> function is used to get the list of all the directories and files present in the current directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.listdir(&#039;C:\\\\Users\\\\HP&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>['.android', '.AndroidStudio3.5', '.crashlytics', '.eclipse', '.emulator_console_auth_token', '.gradle', '.idlerc', '.m2', '.nbi', '.netbeans-derby', '.p2', '.PyCharm2019.3', '.RapidMiner', '.tooling', '.vscode', '3D Objects', 'Anaconda3', 'Anaconda3_1', 'AndroidStudioProjects', 'AppData', 'Application Data', 'Contacts', 'Cookies', 'Desktop', 'Documents', 'Downloads', 'eclipse', 'eclipse-workspace', 'Favorites', 'get-pip.py', 'HP', 'IntelGraphicsProfiles', 'Links', 'Local Settings', 'MicrosoftEdgeBackups']<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Remove a Directory in Python<\/h3>\n\n\n\n<p>The <code><strong>rmdir()<\/strong><\/code> function is used to delete a directory, which is already empty. If the directory is not empty, it won&#8217;t be deleted.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.rmdir(&#039;C:\\\\Users\\\\HP\\\\Pictures\\\\Screenshots\\\\python&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. Check if a Python directory exists<\/h3>\n\n\n\n<p>The <code><strong>os.path.exists(path)<\/strong><\/code> function is used to check whether a particular directory exists or not.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nos.path.exists(&#039;C:\\\\Users\\\\HP\\\\Pictures\\\\Screenshots&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Python os module provides multiple functions to work with directories. We learned how to create, rename, and delete directories in a Python program.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Directory Operations<\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/os.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Directory Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Directory is basically a structure that contains all the corresponding documents, files, and folders. Python&#8217;s os module contains multiple functions for directory management. Python directories functions os.access(path, mode)It uses the uid to check for the path&#8217;s access. os.chdir(path)It changes the CWD to the path specified by the user. os.chflags(path, flags)It is used to set the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2358","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2358","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=2358"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/2358\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=2358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=2358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=2358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}