# How to check if a file is exist in data folder by code?

**URL:** <https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589>\
**Category:** Processing.py\
**Created:** [June 1, 2018, 4:03am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589 "2018-06-01T04:03:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 1, 2018, 4:03am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/1 "2018-06-01T04:03:19Z")

</div>

I have to check if a file is exist in data folder while the code is running. How to achieve that function?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 1, 2018, 4:09am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/2 "2018-06-01T04:09:11Z")

</div>

```auto
File f = dataFile("DoIExist.txt");
String filePath = f.getPath();
boolean exist = f.isFile();
println(filePath, exist);

```

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 1, 2018, 5:03am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/3 "2018-06-01T05:03:22Z")

</div>

It works in java mode. Thanks!! By the way, could you please tell me how to achieve that in python mode if possible?

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 1, 2018, 5:04am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/4 "2018-06-01T05:04:12Z")

</div>

Or is there any function to get current processing data folder’s path?

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [June 1, 2018, 5:12am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/5 "2018-06-01T05:12:08Z")

</div>

You can do that in python like this:

```python
import os

def setup():
    size(400,400)
    print(os.listdir('.'))
    print('someFile.txt' in os.listdir('.'))

```

‘.’ indicates your current path, and ‘..’ goes back one folder up. If you’re storing files in data folder, then you need to check inside `'data/'` instead of ‘.’

Oh and make sure you post your processing.py questions in the appropriate forum.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 1, 2018, 5:17am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/6 "2018-06-01T05:17:03Z")

</div>

> [@newton45](#):
>
> … how to achieve that in Python Mode if possible?

```auto
f = this.dataFile('DoIExist.txt')
filePath, exist = f.path, f.isFile()
print filePath, exist

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 1, 2018, 6:45am UTC](https://discourse.processing.org/t/how-to-check-if-a-file-is-exist-in-data-folder-by-code/589/7 "2018-06-01T06:45:51Z")

</div>

> [@newton45](#):
>
> Or is there any function to get current processing data folder’s path?

Yes! Just pass an empty string `""` to **dataPath()**:

```auto
dataFolderPath = this.dataPath('') + '/'
print dataFolderPath

```

… or to **dataFile()**:

```auto
dir = this.dataFile('')
dataFolderPath = dir.path + '/'
print dataFolderPath

```

However, if you need all files within the sketch’s “data/” folder, like all images for example, you can use **listPaths()**, passing the “data/” folder path + a string w/ all desired “extensions=,”: 🕶

```auto
PICS_EXTS = 'extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp'

dir = this.dataFile('')
imagePaths, imagesFound = (), 0

if dir.isDirectory():
    imagePaths = this.listPaths(dir.path, PICS_EXTS)
    imagesFound = len(imagePaths)

printArray(imagePaths)
print 'Found %d image(s)' % imagesFound

```
