{"id":6798,"date":"2025-01-23T04:59:49","date_gmt":"2025-01-23T04:59:49","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6798"},"modified":"2025-01-23T05:14:41","modified_gmt":"2025-01-23T05:14:41","slug":"how-to-delete-file-or-folder-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/","title":{"rendered":"How to Delete File or Folder in Python"},"content":{"rendered":"\n<p>Sometimes Python developers need to remove one or more files and folders from their system. Generally, they use commands like rm for this purpose. But sometimes you may need to do this programmatically from an application or website. You can easily do this using a Python script. This way you can also trigger file deletion from your Python application or website, at the click of a button or by firing some other event. There are several simple ways to do this. In this article, we will learn how to delete file or folder in Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#How_to_Delete_File_or_Folder_in_Python\" >How to Delete File or Folder in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#1_Using_pathlibPathunlink\" >1. Using pathlib.Path.unlink()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#2_Using_pathlibPathrmdir\" >2. Using pathlib.Path.rmdir()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#3_Using_shutilrmtree\" >3. Using shutil.rmtree()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#4_Using_osremove\" >4. Using os.remove()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#5_Using_osunlink\" >5. Using os.unlink()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#6_Using_osrmdir\" >6. Using os.rmdir()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#7_Delete_Files_with_specific_extension\" >7. Delete Files with specific extension<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#8_Delete_All_Files_in_Folder\" >8. Delete All Files in Folder<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#9_Check_if_File_Exists\" >9. Check if File Exists<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#10_Exception_Handling\" >10. Exception Handling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#Best_Practices\" >Best Practices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Delete_File_or_Folder_in_Python\"><\/span>How to Delete File or Folder in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python provides several modules for file operations. Among them, pathlib, shutil and os are popular ones.  We will use them for our purpose. Here are different ways to remove files and folders in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_pathlibPathunlink\"><\/span>1. Using pathlib.Path.unlink()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you are using Python 3.4 and above, then you can use pathlib module to remove files and folders in Python. pathlib is a library that provides a class-based interface to work with file and folder paths in Python. It provides different functions to perform different kinds of tasks. You can use unlink() function to easily remove file. You just need to pass the path of file to be deleted, as a string. Here is the command to remove file \/home\/ubuntu\/data.txt<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import pathlib<br>pathlib.Path.unlink('\/home\/ubuntu\/data.txt')<\/pre>\n\n\n\n<p>The unlink() function works with both files as well as symbolic links. If you pass path to a symbolic link, it will delete the symbolic link itself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_pathlibPathrmdir\"><\/span>2. Using pathlib.Path.rmdir()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to remove an empty directory or folder in Python, then you can use rmdir() function for this purpose. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pathlib.Path.rmdir(folder_path)<\/pre>\n\n\n\n<p>Here is the command to delete the directory \/home\/ubuntu\/data. Please note, it must be empty.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pathlib.Path.rmdir('\/home\/ubuntu\/data')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_shutilrmtree\"><\/span>3. Using shutil.rmtree()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/shutil.html\">shutil<\/a> is another Python library that offers many high-level functions to easily work with files and directories. You can use it in case you are using Python &lt; 3.4. It features rmtree() function that allows you to easily delete a directory tree. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">shutil.rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None)<\/pre>\n\n\n\n<p>You just need to pass the path to directory. The above command also supports several optional arguments. Among them, ignore_errors, if set to true, then it will ignore any error that occurs during removal of files and folders. If it is set to false, or omitted, then it is handled by error handler specified by onexc or onerror argument.<\/p>\n\n\n\n<p>Here is the command to delete directory tree \/home\/ubuntu\/data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">shutil.rmtree('\/home\/ubuntu\/data')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_osremove\"><\/span>4. Using os.remove()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>OS is another Python library that provides many high level interfaces and functions to interact with underlying OS. It is commonly used to <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-execute-system-command-in-python\/\">run system commands in Python<\/a>. It also provides many useful functions for file operations. Among them, you can use os.remove() function to delete a file. Here is the command for it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br>os.remove('\/home\/ubuntu\/data.txt')<\/pre>\n\n\n\n<p>If the above mentioned file does not exist, then it will raise FileNotFoundError. In fact, on Windows systems, if the file is open, even then you will get an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_osunlink\"><\/span>5. Using os.unlink()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to remove a symbolic link, you can use os.unlink() function. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.unlink(path, *, dir_fd = None) <\/pre>\n\n\n\n<p>The file path is the first argument and it is mandatory. The * after that refers to optional arguments. dir_fd is also an optional argument that refers to a directory path. If absolute file path is mentioned as first argument, then this argument is ignored. <\/p>\n\n\n\n<p>Here is the command to remove a symbolic link \/home\/ubuntu\/test.txt.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.unlink('\/home\/ubuntu\/test.txt')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Using_osrmdir\"><\/span>6. Using os.rmdir()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use os.rmdir() function to remove an empty directory. Its syntax is similar to that of shutil.rmtree().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.rmdir(path, *, dir_fd = None)  <\/pre>\n\n\n\n<p>In the above command, directory path is the first argument, followed by optional arguments. Among them, dir_fd points to a directory path. Its default value is None. It is ignored if absolute directory path is provided in first argument.<\/p>\n\n\n\n<p>Here is the command to delete directory \/home\/ubuntu\/data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.rmdir('\/home\/ubuntu\/data')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Delete_Files_with_specific_extension\"><\/span>7. Delete Files with specific extension<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us look at a common use case where you need to delete all files in a directory with a specific extension. Here is the code to do this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os <br>from os import listdir<br><br>my_path = '\/home\/ubuntu\/data'<br>for file_name in listdir(my_path):<br>    if file_name.endswith('.txt'):<br>        os.remove(my_path + file_name)<\/pre>\n\n\n\n<p>In the above code, we import listdir function to get a list of all files in a directory, as a list. We loop through this list and for each file we call endswith() function to check its extension. We see if the extension is .txt or not. If it is true, then we call os.remove() function on the full file path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_Delete_All_Files_in_Folder\"><\/span>8. Delete All Files in Folder<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So far we have seen commands to delete only one file in a directory. What if you need to delete all files in a directory? For this purpose, you can use the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os, glob<br><br># Loop over files &amp; delete them individually<br>my_path = \"\/home\/ubuntu\/data\"<br>for file in glob.glob(my_path):<br>    os.remove(file)<\/pre>\n\n\n\n<p>In the above code, we import glob module to retrieve file and directory paths recursively, as a list. We loop through this list and call os.remove() function on each file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"9_Check_if_File_Exists\"><\/span>9. Check if File Exists<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Most of the functions we have seen above, require that the file to be deleted, actually exists. However, if the file does not exist, then these functions will throw an Exception. So it is advisable to first <a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\">check if file exists<\/a>, before you delete it. For this purpose, you can call os.path.isfile() function. It will accept a file path as argument, and return true if file actually exists at this path. Else it will return false.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br><br>my_path = \"\/home\/ubuntu\/data.txt\"<br># File exists<br>if os.path.isfile(my_path):<br>    os.remove(my_path)<br>else:<br>    # File does not exist<br>    print(\"Error: %s file not found\" % my_path)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"10_Exception_Handling\"><\/span>10. Exception Handling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Another solution to use in case the file exists, is to use exception handling. When a file does not exist, most of the above mentioned functions throw an Exception. We can handle this exception to make sure the code continues to run.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import os<br><br># path to file<br>my_path = '\/home\/ubuntu\/data.txt'<br><br># Delete file<br>try:<br>    os.remove(my_path)<br>except OSError as e:<br>    # File does not exist<br>    print(\"Error: %s - %s.\" % (e.filename, e.strerror))<\/pre>\n\n\n\n<p>In the above code, we call os.remove() function on file path. If the file does not exist at the given path, it throws an exception, which is handled.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Best_Practices\"><\/span>Best Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are several best practices you can follow while to delete file or folder.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Make sure the file exists. Otherwise, most of the functions mentioned above will throw an error.<\/li>\n\n\n\n<li>Ensure that the user running these functions has appropriate permissions. Otherwise, you will get an error.<\/li>\n\n\n\n<li>Before deletion, check if file exists using isfile() or some other function. It is a much better way than handling exceptions.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt how to delete file or folder in Python. You can use any of them as per your requirement. But please note, the system user that runs these Python functions must have the permission to delete the files &amp; folders. Secondly, these files &amp; folders must actually exist on your disk. Otherwise, you will get an error. Also, most of these commands do not ask for a confirmation before deleting file or directory. Once the file or folder is deleted, there is no way to recover them. So make sure you provide the correct file path as arguments.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-copy-files-in-python\/\">How to Copy Files in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-rows-in-pandas-dataframe\/\">How to Iterate Over Rows in Pandas DataFrame<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-iterate-over-dictionaries-using-for-loops\/\">How to Iterate Over Dictionaries Using Loops<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often Python developers need to remove files and folders. Here is how to delete file or folder in Python. <\/p>\n","protected":false},"author":1,"featured_media":6834,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[378,379],"class_list":["post-6798","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-delete-file","tag-delete-folder"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Delete File or Folder in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Delete File or Folder in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-23T04:59:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-23T05:14:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"275\" \/>\n\t<meta property=\"og:image:height\" content=\"184\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Delete File or Folder in Python\",\"datePublished\":\"2025-01-23T04:59:49+00:00\",\"dateModified\":\"2025-01-23T05:14:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/\"},\"wordCount\":1182,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1\",\"keywords\":[\"delete file\",\"delete folder\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/\",\"name\":\"How to Delete File or Folder in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1\",\"datePublished\":\"2025-01-23T04:59:49+00:00\",\"dateModified\":\"2025-01-23T05:14:41+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/01\\\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1\",\"width\":275,\"height\":184,\"caption\":\"delete file or folder in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-delete-file-or-folder-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Delete File or Folder in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Delete File or Folder in Python - Ubiq BI","description":"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.","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:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Delete File or Folder in Python - Ubiq BI","og_description":"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-01-23T04:59:49+00:00","article_modified_time":"2025-01-23T05:14:41+00:00","og_image":[{"width":275,"height":184,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Delete File or Folder in Python","datePublished":"2025-01-23T04:59:49+00:00","dateModified":"2025-01-23T05:14:41+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/"},"wordCount":1182,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1","keywords":["delete file","delete folder"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/","name":"How to Delete File or Folder in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1","datePublished":"2025-01-23T04:59:49+00:00","dateModified":"2025-01-23T05:14:41+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often Python developers need to remove files and directories. Here is how to delete file or folder in Python.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1","width":275,"height":184,"caption":"delete file or folder in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-delete-file-or-folder-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Delete File or Folder in Python"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/01\/delete-files-folders-python.jpg?fit=275%2C184&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1LE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=6798"}],"version-history":[{"count":45,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6798\/revisions"}],"predecessor-version":[{"id":6845,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6798\/revisions\/6845"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6834"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}