{"id":719,"date":"2020-10-23T00:43:22","date_gmt":"2020-10-23T00:43:22","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=719"},"modified":"2025-03-30T11:58:59","modified_gmt":"2025-03-30T11:58:59","slug":"python-check-if-file-exists","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-check-if-file-exists\/","title":{"rendered":"Python Check If File Exists"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the <code>os.path.exists()<\/code> and <code>pathlib.is_file()<\/code> functions to check if a file exists.<\/p>\n\n\n\n<p>When processing files, you&#8217;ll often want to check if a file exists before doing something else with it such as <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-read-text-file\/\">reading from the file<\/a> or <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-write-text-file\/\">writing data to it<\/a>.<\/p>\n\n\n\n<p>To check if a file exists, you can use the <code>exists()<\/code> function from the <code>os.path<\/code> module: <\/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\">from<\/span> os.path <span class=\"hljs-keyword\">import<\/span> exists\n\nfile_exists = exists(path_to_file)<\/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>Alternatively, you can use the <code>is_file()<\/code> method from the <code>Path<\/code> class from the <code>pathlib<\/code> module:<\/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\">from<\/span> pathlib <span class=\"hljs-keyword\">import<\/span> Path\n\npath = Path(path_to_file)\n\npath.is_file()<\/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='checking-if-a-file-exists-using-os-path-exists-function'>Checking if a file exists using os.path.exists() function <a href=\"#checking-if-a-file-exists-using-os-path-exists-function\" class=\"anchor\" id=\"checking-if-a-file-exists-using-os-path-exists-function\" title=\"Anchor for Checking if a file exists using os.path.exists() function\">#<\/a><\/h2>\n\n\n\n<p>To check if a file exists, you pass the file path to the <code>exists()<\/code> function from the <code>os.path<\/code> standard library.<\/p>\n\n\n\n<p>First, import the <code>os.path<\/code> standard library:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> os.path<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>Second, call the <code>exists()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">os<\/span><span class=\"hljs-selector-class\">.path<\/span><span class=\"hljs-selector-class\">.exists<\/span>(<span class=\"hljs-selector-tag\">path_to_file<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the file exists, the <code>exists()<\/code> function returns <code>True<\/code>. Otherwise, it returns <code>False<\/code>.<\/p>\n\n\n\n<p>If the file is in the same folder as the program, the <code>path_to_file<\/code> is just simply the file name. <\/p>\n\n\n\n<p>However, it&#8217;s not the case, you need to pass the full file path of the file. For example:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">\/path\/to\/filename<\/code><\/span><\/pre>\n\n\n<p>Even if you run the program on Windows, you should use the forward-slash (<code>\/<\/code>) to separate the path. It&#8217;ll work across Windows, macOS, and Linux.<\/p>\n\n\n\n<p>The following example uses the <code>exists()<\/code> function to check if the <code>readme.txt<\/code> file exists in the same folder as the program:<\/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.path\n\nfile_exists = os.path.exists(<span class=\"hljs-string\">'readme.txt'<\/span>)\n\nprint(file_exists)<\/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<p>If the <code>readme.txt<\/code> file exists, you&#8217;ll see the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><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<p>Otherwise, you&#8217;ll see <code>False<\/code> on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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<p>To make the call to the <code>exists()<\/code> function shorter and more obvious, you can import that function and rename it to <code>file_exists()<\/code> function like this:<\/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\">from<\/span> os.path <span class=\"hljs-keyword\">import<\/span> exists <span class=\"hljs-keyword\">as<\/span> file_exists\n\nfile_exists(<span class=\"hljs-string\">'readme.txt'<\/span>)<\/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='checking-if-a-file-exists-using-the-pathlib-module'>Checking if a file exists using the pathlib module <a href=\"#checking-if-a-file-exists-using-the-pathlib-module\" class=\"anchor\" id=\"checking-if-a-file-exists-using-the-pathlib-module\" title=\"Anchor for Checking if a file exists using the pathlib module\">#<\/a><\/h2>\n\n\n\n<p>The <code>pathlib<\/code> module allows you to manipulate files and directories using the object-oriented approach. If you&#8217;re not familiar with object-oriented programming, check out the <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/\">Python OOP<\/a> section.<\/p>\n\n\n\n<p class=\"note\">Python introduced the <code><a href=\"https:\/\/www.pythontutorial.net\/python-standard-library\/python-path\/\">pathlib<\/a><\/code> module since the version 3.4. So you should have Python 3.4 or later to use the module.<\/p>\n\n\n\n<p>First, import the <code>Path<\/code> class from the <code>pathlib<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> pathlib <span class=\"hljs-keyword\">import<\/span> Path<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>Then, instantiate a new instance of the <code>Path<\/code> class and initialize it with the file path that you want to check for existence:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">path = Path(path_to_file)<\/code><\/span><\/pre>\n\n\n<p>Finally, check if the file exists using the <code>is_file()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">path<\/span><span class=\"hljs-selector-class\">.is_file<\/span>()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the file doesn&#8217;t exist, the <code>is_file()<\/code> method returns <code>False<\/code>. Otherwise, it returns <code>True<\/code>.<\/p>\n\n\n\n<p>The following example shows how to use the <code>Path<\/code> class from the <code>pathlib<\/code> module to check if the <code>readme.txt<\/code> file exists in the same folder of the program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">from pathlib import Path\n\npath_to_file = <span class=\"hljs-string\">'readme.txt'<\/span>\npath = Path(path_to_file)\n\n<span class=\"hljs-keyword\">if<\/span> path.is_file():\n    <span class=\"hljs-keyword\">print<\/span>(f<span class=\"hljs-string\">'The file {path_to_file} exists'<\/span>)\n<span class=\"hljs-keyword\">else<\/span>:\n    <span class=\"hljs-keyword\">print<\/span>(f<span class=\"hljs-string\">'The file {path_to_file} does not exist'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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<p>If the <code>readme.txt<\/code> file exists, you&#8217;ll see the following output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">The<\/span> <span class=\"hljs-selector-tag\">file<\/span> <span class=\"hljs-selector-tag\">readme<\/span><span class=\"hljs-selector-class\">.txt<\/span> <span class=\"hljs-selector-tag\">exists<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/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 <code>os.path.exists()<\/code> function or <code>Path.is_file()<\/code> method to check if a file exists<\/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=check-file-exists\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\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=\"719\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-check-if-file-exists\/\"\n\t\t\t\tdata-post-title=\"Python Check If File Exists\"\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=\"719\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-check-if-file-exists\/\"\n\t\t\t\tdata-post-title=\"Python Check If File Exists\"\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>Summary: in this tutorial, you&#8217;ll learn how to use the os.path.exists() and pathlib.is_file() functions to check if a file exists. When processing files, you&#8217;ll often want to check if a file exists before doing something else with it such as reading from the file or writing data to it. To check if a file exists, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":68,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-719","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/719","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=719"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/719\/revisions"}],"predecessor-version":[{"id":7228,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/719\/revisions\/7228"}],"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=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}