{"id":2464,"date":"2021-07-07T15:34:50","date_gmt":"2021-07-07T15:34:50","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2464"},"modified":"2025-03-30T13:01:01","modified_gmt":"2025-03-30T13:01:01","slug":"python-directory","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-directory\/","title":{"rendered":"How to Manipulate Directories in Python"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to manipulate directories in Python using the <code>os<\/code> module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='get-the-current-working-directory'>Get the current working directory <a href=\"#get-the-current-working-directory\" class=\"anchor\" id=\"get-the-current-working-directory\" title=\"Anchor for Get the current working directory\">#<\/a><\/h2>\n\n\n\n<p>The current working directory is the directory where the Python script is running. To get the current working directory, you use the <code>os.getcwd()<\/code> as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\n\ncwd = os.getcwd()\nprint(cwd)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To change the current working directory, you use the function <code>os.chdir()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\n\nos.chdir(<span class=\"hljs-string\">'\/script'<\/span>)\ncwd = os.getcwd()\nprint(cwd)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='join-and-split-a-path'>Join and split a path <a href=\"#join-and-split-a-path\" class=\"anchor\" id=\"join-and-split-a-path\" title=\"Anchor for Join and split a path\">#<\/a><\/h2>\n\n\n\n<p>To make a program work across platforms including Windows, Linux, and macOS, you need to use platform-independent file and directory paths. <\/p>\n\n\n\n<p>Python provides you with a submodule <code>os.path<\/code> that contains several useful functions and constants to join and split paths.<\/p>\n\n\n\n<p>The <code>join()<\/code> function joins path components together and returns a path with the corresponding path separator. For example, it uses backslash (<code>\\<\/code>) on Windows and forward slash (<code>\/<\/code>) on macOS or Linux.<\/p>\n\n\n\n<p>The <code>split()<\/code> function splits a path into components without a path separator. Here&#8217;s an example of using <code>join()<\/code> and <code>split()<\/code> functions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import os\n\nfp = os.path.join(<span class=\"hljs-string\">'temp'<\/span>, <span class=\"hljs-string\">'python'<\/span>)\n<span class=\"hljs-keyword\">print<\/span>(fp)  <span class=\"hljs-comment\"># temp\\python (on Windows)<\/span>\n\npc = os.path.split(fp)\n<span class=\"hljs-keyword\">print<\/span>(pc)  <span class=\"hljs-comment\"># ('temp', 'python')<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='test-if-a-path-is-a-directory'>Test if a path is a directory <a href=\"#test-if-a-path-is-a-directory\" class=\"anchor\" id=\"test-if-a-path-is-a-directory\" title=\"Anchor for Test if a path is a directory\">#<\/a><\/h2>\n\n\n\n<p>To check if a path exists and is a directory, you can use the functions <code>os.path.exists()<\/code> and <code>os.path.isdir()<\/code> functions. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import os\n\ndir = os.path.join(<span class=\"hljs-string\">\"C:\\\\\"<\/span>, <span class=\"hljs-string\">\"temp\"<\/span>)\n<span class=\"hljs-keyword\">print<\/span>(dir)\n\n<span class=\"hljs-keyword\">if<\/span> os.path.exists(dir) <span class=\"hljs-keyword\">or<\/span> os.path.isdir(dir):\n    <span class=\"hljs-keyword\">print<\/span>(f<span class=\"hljs-string\">'The {dir} is a directory'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='create-a-directory'>Create a directory <a href=\"#create-a-directory\" class=\"anchor\" id=\"create-a-directory\" title=\"Anchor for Create a directory\">#<\/a><\/h2>\n\n\n\n<p>To create a new directory, you use <code>os.mkdir()<\/code> function. And you should always check if a directory exists first before creating a new directory. <\/p>\n\n\n\n<p>The following example creates a new directory called <code>python<\/code> under the <code>c:\\temp<\/code> directory.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\ndir = os.path.join(<span class=\"hljs-string\">\"C:\\\\\"<\/span>, <span class=\"hljs-string\">\"temp\"<\/span>, <span class=\"hljs-string\">\"python\"<\/span>)\n<span class=\"hljs-keyword\">if<\/span> not os.path.exists(dir):\n    os.mkdir(dir)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='rename-a-directory'>Rename a directory <a href=\"#rename-a-directory\" class=\"anchor\" id=\"rename-a-directory\" title=\"Anchor for Rename a directory\">#<\/a><\/h2>\n\n\n\n<p>To rename the directory, you use the <code>os.rename()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\noldpath = os.path.join(<span class=\"hljs-string\">\"C:\\\\\"<\/span>, <span class=\"hljs-string\">\"temp\"<\/span>, <span class=\"hljs-string\">\"python\"<\/span>)\nnewpath = os.path.join(<span class=\"hljs-string\">\"C:\\\\\"<\/span>, <span class=\"hljs-string\">\"temp\"<\/span>, <span class=\"hljs-string\">\"python3\"<\/span>)\n\n<span class=\"hljs-keyword\">if<\/span> os.path.exists(oldpath) and not os.path.exists(newpath):\n    os.rename(oldpath, newpath)\n    print(<span class=\"hljs-string\">\"'{0}' was renamed to '{1}'\"<\/span>.format(oldpath, newpath))\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='delete-a-directory'>Delete a directory <a href=\"#delete-a-directory\" class=\"anchor\" id=\"delete-a-directory\" title=\"Anchor for Delete a directory\">#<\/a><\/h2>\n\n\n\n<p>To delete a directory, you use the  <code>os.rmdir()<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\ndir = os.path.join(<span class=\"hljs-string\">\"C:\\\\\"<\/span>,<span class=\"hljs-string\">\"temp\"<\/span>,<span class=\"hljs-string\">\"python\"<\/span>)\n<span class=\"hljs-keyword\">if<\/span> os.path.exists(dir):\n    os.rmdir(dir)\n    print(dir + <span class=\"hljs-string\">' is removed.'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='traverse-a-directory-recursively'>Traverse a directory recursively <a href=\"#traverse-a-directory-recursively\" class=\"anchor\" id=\"traverse-a-directory-recursively\" title=\"Anchor for Traverse a directory recursively\">#<\/a><\/h2>\n\n\n\n<p>The <code>os.walk()<\/code> function allows you to traverse a directory recursively. The <code>os.walk()<\/code> function returns the root directory, the sub-directories, and files. <\/p>\n\n\n\n<p>The following example shows how to print all files and directories in the <code>c:\\temp<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os\n\npath = <span class=\"hljs-string\">\"c:\\\\temp\"<\/span>\n<span class=\"hljs-keyword\">for<\/span> root, dirs, files <span class=\"hljs-keyword\">in<\/span> os.walk(path):\n    print(<span class=\"hljs-string\">\"{0} has {1} files\"<\/span>.format(root, len(files)))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>os.getcwd()<\/code> function to get the current working directory.<\/li>\n\n\n\n<li>Use the <code>os.chdir()<\/code> function to change the current working directory to a new one.<\/li>\n\n\n\n<li>Use the <code>os.mkdir()<\/code> function to make a new directory.<\/li>\n\n\n\n<li>Use the <code>os.rename()<\/code> function to rename a directory.<\/li>\n\n\n\n<li>Use the <code>os.rmdir()<\/code> function to remove a directory.<\/li>\n\n\n\n<li>Use the <code>os.walk()<\/code> function to list the contents of a directory.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=directory\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2464\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-directory\/\"\n\t\t\t\tdata-post-title=\"How to Manipulate Directories in Python\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2464\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-directory\/\"\n\t\t\t\tdata-post-title=\"How to Manipulate Directories in Python\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to manipulate directories in Python using the os module.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":73,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2464","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2464"}],"version-history":[{"count":2,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2464\/revisions"}],"predecessor-version":[{"id":7240,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2464\/revisions\/7240"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}