<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to check file exists in databricks in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/57007#M30714</link>
    <description>&lt;P&gt;How to check if a file exists in DBFS?&lt;/P&gt;&lt;P&gt;Let's write a Python function to check if the file exists or not&lt;/P&gt;&lt;P&gt;-------------------------------------------------------------&lt;/P&gt;&lt;P&gt;def file_exists(path):&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; try:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dbutils.fs.ls(path)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return True&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; except Exception as e:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 'java.io.FileNotFoundException' in str(e):&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return False&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; raise&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result = file_exists("dbfs:/Repos/")&lt;/P&gt;&lt;P&gt;print(Result)&lt;/P&gt;&lt;P&gt;"True" represents that path exists&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amit_Dass_0-1705019439413.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/5794iD8BDBE6A34616A5B/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Amit_Dass_0-1705019439413.png" alt="Amit_Dass_0-1705019439413.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 12 Jan 2024 00:31:01 GMT</pubDate>
    <dc:creator>Amit_Dass</dc:creator>
    <dc:date>2024-01-12T00:31:01Z</dc:date>
    <item>
      <title>How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27945#M19783</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I Have a while loop there i have to check a file exists or not if exists read the file in a data frame else go to another file &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2019 07:34:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27945#M19783</guid>
      <dc:creator>SimhadriRaju</dc:creator>
      <dc:date>2019-07-25T07:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27946#M19784</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;There might be other ways to do this, but this works. &lt;/P&gt;
&lt;P&gt;@Simhadri Raju &lt;/P&gt;
&lt;P&gt;Basically, &lt;/P&gt;
&lt;P&gt;I use dbutils.fs.head here, but anything that throws an exception if it fails to find the file would work. &lt;/P&gt;
&lt;P&gt;So I go to read the first byte of the file with &lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;dbutils.fs.head(arg1,1)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If that throws an exception I return False&lt;/P&gt;
&lt;P&gt;If that succeeds I return True. &lt;/P&gt;
&lt;P&gt;Put that in a function, call the function with your filename and you are good to go. &lt;/P&gt;
&lt;P&gt;Full code here&lt;/P&gt;## Function to check to see if a file exists
&lt;P&gt;&lt;/P&gt; 
&lt;P&gt;def fileExists (arg1): try: dbutils.fs.head(arg1,1) except: return False; else: return True;&lt;/P&gt; 
&lt;B&gt; Calling that function with your filename&lt;/B&gt; 
&lt;P&gt;ilename = &amp;lt;pathtoyourfile&amp;gt;&lt;/P&gt; 
&lt;P&gt;if(fileExists(filename)): print("Yes it exists");&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 04:02:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27946#M19784</guid>
      <dc:creator>User16857282152</dc:creator>
      <dc:date>2019-10-31T04:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27947#M19785</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Oh that code did not render well, &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2019 04:03:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27947#M19785</guid>
      <dc:creator>User16857282152</dc:creator>
      <dc:date>2019-10-31T04:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27948#M19786</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Here's a Python solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;def file_exists(path):
  try:
    dbutils.fs.ls(path)
    return True
  except Exception as e:
    if 'java.io.FileNotFoundException' in str(e):
      return False
    else:
      raise&lt;/CODE&gt;&lt;/PRE&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2019 09:49:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27948#M19786</guid>
      <dc:creator>zerogjoe</dc:creator>
      <dc:date>2019-12-10T09:49:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27949#M19787</link>
      <description>&lt;P&gt;Very many thanks to @zerogjoe​&amp;nbsp; for his elegant answer, which works perfectly for Databricks formatted file paths.&lt;/P&gt;&lt;P&gt;To make this a little more robust and allow for filesystem api paths (that can be used with os, glob etc and start with "/dbfs") I've added a few lines of code.&lt;/P&gt;&lt;P&gt;def exists(path): """ Check for existence of path within Databricks file system. """&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;if path[:5] == "/dbfs":
     import os
     return os.path.exists(path)
 else:
     try:
         dbutils.fs.ls(path)
         return True
     except Exception as e:
         if 'java.io.FileNotFoundException' in str(e):
             return False
         else:
             raise&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 04 May 2020 16:32:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27949#M19787</guid>
      <dc:creator>dughub</dc:creator>
      <dc:date>2020-05-04T16:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27950#M19788</link>
      <description>&lt;P&gt;If you are looking for a scala solution, here it is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;def pathExists(tablePath: String): Boolean = {
  try{
    dbutils.fs.ls(tablePath)
    return true
  } catch {
    case e: java.io.FileNotFoundException =&amp;gt; println("Given path cannot be located")
    return false
  }
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Dec 2021 20:26:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/27950#M19788</guid>
      <dc:creator>Hari_Gopinath</dc:creator>
      <dc:date>2021-12-01T20:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/50502#M28820</link>
      <description>&lt;P&gt;You can do so by running the snippet below, which uses the new [databricks python SDK](&lt;A href="https://github.com/databricks/databricks-sdk-py/" target="_blank"&gt;https://github.com/databricks/databricks-sdk-py/&lt;/A&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Install the package:&lt;BR /&gt;`pip install databricks-sdk`&lt;/P&gt;&lt;P&gt;Python snippet:&lt;BR /&gt;```python&lt;BR /&gt;from databricks.sdk import WorkspaceClient&lt;/P&gt;&lt;P&gt;# Remember to change the arguments below&lt;BR /&gt;w_client = WorkspaceClient(host="my_host", token="my_db_tokens")&lt;/P&gt;&lt;P&gt;# add an absolute path&lt;BR /&gt;dbfs_path_exist = w_client.dbfs.exists('/dbfs_my_path')&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;Hope it helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 11:15:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/50502#M28820</guid>
      <dc:creator>Mustious</dc:creator>
      <dc:date>2023-11-06T11:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to check file exists in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/57007#M30714</link>
      <description>&lt;P&gt;How to check if a file exists in DBFS?&lt;/P&gt;&lt;P&gt;Let's write a Python function to check if the file exists or not&lt;/P&gt;&lt;P&gt;-------------------------------------------------------------&lt;/P&gt;&lt;P&gt;def file_exists(path):&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; try:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dbutils.fs.ls(path)&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return True&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; except Exception as e:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if 'java.io.FileNotFoundException' in str(e):&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return False&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; raise&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Result = file_exists("dbfs:/Repos/")&lt;/P&gt;&lt;P&gt;print(Result)&lt;/P&gt;&lt;P&gt;"True" represents that path exists&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Amit_Dass_0-1705019439413.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/5794iD8BDBE6A34616A5B/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Amit_Dass_0-1705019439413.png" alt="Amit_Dass_0-1705019439413.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jan 2024 00:31:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-check-file-exists-in-databricks/m-p/57007#M30714</guid>
      <dc:creator>Amit_Dass</dc:creator>
      <dc:date>2024-01-12T00:31:01Z</dc:date>
    </item>
  </channel>
</rss>

