{"id":5142,"date":"2024-10-17T06:34:08","date_gmt":"2024-10-17T06:34:08","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=5142"},"modified":"2024-10-17T06:34:44","modified_gmt":"2024-10-17T06:34:44","slug":"how-to-check-if-file-exists-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/","title":{"rendered":"How to Check if File Exists in Python"},"content":{"rendered":"\n<p>Often Python applications need to work with one or more files as a part of their features. Sometimes, they need to create new files, modify existing files or even overwrite files. It is essential to check if a file exists or not before working with it. Otherwise, your application may request a non existent file and throw an error. In this article, we will learn how to check if file exists 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-check-if-file-exists-in-python\/#Why_Check_If_File_Exists_in_Python\" >Why Check If File Exists 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-check-if-file-exists-in-python\/#Different_Ways_to_Check_if_File_Exists_in_Python\" >Different Ways to Check if File Exists in Python<\/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-check-if-file-exists-in-python\/#1_Using_ospathexists\" >1. Using os.path.exists()<\/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-check-if-file-exists-in-python\/#2_Using_ospathisfile\" >2. Using os.path.isfile()<\/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-check-if-file-exists-in-python\/#3_Using_ospathisdir\" >3. Using os.path.isdir()<\/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-check-if-file-exists-in-python\/#4_Using_pathlibPathexists\" >4. Using pathlib.Path.exists()<\/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-check-if-file-exists-in-python\/#Conclusion\" >Conclusion<\/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-check-if-file-exists-in-python\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_Check_If_File_Exists_in_Python\"><\/span>Why Check If File Exists in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When your Python script or program accesses a file, it needs to be actually present and you need to have the adequate permissions to open it. Otherwise, Python will throw an Exception and your code will stop working. This will completely throw your application out of gear and stop all the processing its doing. Therefore, it is important to check if file exists in Python, before you can even start working with it. <\/p>\n\n\n\n<p>Typically, Python developers wrap the code that accesses files, inside a try&#8230;except block. But it can be tedious to allow Python to throw an exception and then handle it elsewhere. A much cleaner way is to directly check if the file exists, before you even start processing it. Python provides many different functions for this purpose, that allow you to check if file or directory exists, without throwing an exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Different_Ways_to_Check_if_File_Exists_in_Python\"><\/span>Different Ways to Check if File Exists in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are many different ways to check if a file or directory exists in Python. We will look at the 4 most common solutions to this problem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_ospathexists\"><\/span>1. Using os.path.exists()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/docs.python.org\/3\/library\/os.html\" target=\"_blank\" rel=\"noreferrer noopener\">OS library<\/a> is a built-in module available with every Python installation. It provides many useful functions for Python interpreter to easily interact with OS and use its functions. Among them, os.path is a popular submodule that allows you to work with file paths and directories. It provides many different functions to check if file exists or not. You can use os.path.exists() function for this purpose. Here is its syntax. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.path.exists(file_path)<\/pre>\n\n\n\n<p>In the above example, you can provide the absolute or relative path to your file. It returns true if the path exists, else it returns false.<\/p>\n\n\n\n<p>Please note, if you provide relative path, then this function will look for the file relative to the directory in which this script is located.<\/p>\n\n\n\n<p>Here is an example to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import os<br>>>> pth = '\/home\/ubuntu'<br>>>> os.path.exists(pth)<br>False<br><br>>>> pth = '\/home\/ubuntu\/test.jpg'<br>>>> os.path.exists(pth)<br>True<\/pre>\n\n\n\n<p>In the first case, we provide a directory path and in the second case we provide a file path. So in the first case, it returns False and in the second case it returns True.<\/p>\n\n\n\n<p>Please note, if you are running these commands on Windows, then you may need to escape the backslash in your file paths, to avoid being misinterpreted. Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import os<br>>>> pth = 'C:\\test.jpg'<br>>>> os.path.exists(pth)<br>False<br><br>>>> pth = 'C:\\\\test.jpg'<br>>>> os.path.exists(pth)<br>True<\/pre>\n\n\n\n<p>Also, if you pass a symbolic link to file path that exists, then os.path.exists() will return True. Symbolic links are treated just as actual paths. Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> pth = \"\/home\/ubuntu\"<br>>>> os.symlink(dirname, pth) <br>>>> os.path.exists(dirname)<br>False<br>  <br>>>> pth = \"\/home\/ubuntu\/test.jpg\"<br>>>> os.symlink(dirname, pth) <br>>>> os.path.exists(dirname)<br>True<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_ospathisfile\"><\/span>2. Using os.path.isfile()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use os.path.isfile() function to check if a specified path points to a file or not. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.path.isfile(file_path)<\/pre>\n\n\n\n<p>Here is an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import os<br>>>> pth = '\/home\/ubuntu'<br>>>> os.path.isfile(pth)<br>False<br><br>>>> pth = '\/home\/ubuntu\/test.jpg'<br>>>> os.path.isfile(pth)<br>True<\/pre>\n\n\n\n<p>Here also, you need to escape the backslashes in your file path, if you are using Windows. Also, symbolic links are treated exactly as actual paths to files and directories.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_ospathisdir\"><\/span>3. Using os.path.isdir()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>On the other hand, if you want to check if a given path is a directory, you can use os.path.isdir() function. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">os.path.isdir(directory_path)<\/pre>\n\n\n\n<p>Here is an example to illustrate it. It works opposite to what os.path.exists() and os.path.isfile(). It returns True if the path points to a directory, and False if it points to a file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import os<br>>>> pth = '\/home\/ubuntu'<br>>>> os.path.isfile(pth)<br>True<br><br>>>> pth = '\/home\/ubuntu\/test.jpg'<br>>>> os.path.isfile(pth)<br>False<\/pre>\n\n\n\n<p>In this case also, you need to escape the backslash in your file paths, if you are using Windows. Also, symbolic links work the same way as actual paths. So if your directory exists, then the symbolic link to it will return True, else False.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_pathlibPathexists\"><\/span>4. Using pathlib.Path.exists()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Pathlib is another standard Python library that allows you to work with filesystem. It supports object oriented filesystem paths that are classified into pure paths that support only computation operations but not I\/O operations, and concrete paths that support both computational and I\/O operations. <\/p>\n\n\n\n<p>It also provides pathlib.Path.exists() function that checks if a file path exists or not. If it exists, the function returns True, else it returns False.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">>>> import pathlib<br><br>>>> path = '\/home\/ubuntu'<br>>>> pathlib.Path.exists(path)<br>False<br><br>>>> path = '\/home\/ubuntu\/test.jpg'<br>>>> pathlib.Path.exists(path)<br>True<\/pre>\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 several ways to check if file exists in Python. You can use any of them as per your requirement. Please ensure that the user that runs this code actually has the permission to access the file, otherwise you will get always get False. If you are using Windows, please remember to escape the backslashes in your file paths. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. Why Check If File Exists in Python<\/strong><\/p>\n\n\n\n<p>If you try to access a file that does not exist, in your Python script, then it will give an error and throw a FileNotFound Exception.<\/p>\n\n\n\n<p><strong>2. When to Check File existence?<\/strong><\/p>\n\n\n\n<p>It is best to check for file existence before you open the file to avoid any errors and exceptions.<\/p>\n\n\n\n<p><strong>3. Can I check multiple files at once?<\/strong><\/p>\n\n\n\n<p>The functions os.path.exists(), os.path.isfile(), os.path.isdir() and pathlib.Path.exists() all work on only 1 file at a time. If you want to check the existence of multiple files at once, you can loop through a list of file paths and call any of these functions in each iteration, on one file path at a time.<\/p>\n\n\n\n<p><strong>4. What are common mistakes while checking file existence?<\/strong><\/p>\n\n\n\n<p>Please make sure you provide the right path and the system user that runs the Python script has permission to access the file. Also, if you are providing file path on Windows, then you need to escape backslash by adding an extra backslash (\\\\). Also, remember that os.path.isdir() works only for directories and not files whereas the other 3 functions os.path.exists(), os.path.isfile() and pathlib.Path.exists() work for only directories.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-merge-two-dictionaries-in-python\/\">How to Merge Two Dictionaries in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-split-list-into-even-chunks-in-python\/\">How to Split List into Even Chunks in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-backup-mysql-database-in-python\/\">How to Backup MySQL Database in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not in Python.<\/p>\n","protected":false},"author":1,"featured_media":5163,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[322],"class_list":["post-5142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-file-exists"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Check if File Exists in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not 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-check-if-file-exists-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 Check if File Exists in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-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=\"2024-10-17T06:34:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-17T06:34:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"308\" \/>\n\t<meta property=\"og:image:height\" content=\"204\" \/>\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=\"5 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-check-if-file-exists-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Check if File Exists in Python\",\"datePublished\":\"2024-10-17T06:34:08+00:00\",\"dateModified\":\"2024-10-17T06:34:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\"},\"wordCount\":1047,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1\",\"keywords\":[\"file exists\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\",\"url\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\",\"name\":\"How to Check if File Exists in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1\",\"datePublished\":\"2024-10-17T06:34:08+00:00\",\"dateModified\":\"2024-10-17T06:34:44+00:00\",\"author\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1\",\"width\":308,\"height\":204,\"caption\":\"check if file exists in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ubiq.co\/tech-blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Check if File Exists 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 Check if File Exists in Python - Ubiq BI","description":"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not 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-check-if-file-exists-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Check if File Exists in Python - Ubiq BI","og_description":"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not in Python.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-10-17T06:34:08+00:00","article_modified_time":"2024-10-17T06:34:44+00:00","og_image":[{"width":308,"height":204,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Check if File Exists in Python","datePublished":"2024-10-17T06:34:08+00:00","dateModified":"2024-10-17T06:34:44+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/"},"wordCount":1047,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1","keywords":["file exists"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/","name":"How to Check if File Exists in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1","datePublished":"2024-10-17T06:34:08+00:00","dateModified":"2024-10-17T06:34:44+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"It is important to check if file exists in Python before accessing it. Here are different ways to check whether file exists or not in Python.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1","width":308,"height":204,"caption":"check if file exists in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-check-if-file-exists-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Check if File Exists 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\/2024\/10\/check-if-file-exists-in-python.jpg?fit=308%2C204&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1kW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5142","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=5142"}],"version-history":[{"count":25,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5142\/revisions"}],"predecessor-version":[{"id":5168,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/5142\/revisions\/5168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/5163"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=5142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=5142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=5142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}