I have to check if a file is exist in data folder while the code is running. How to achieve that function?
1 Like
File f = dataFile("DoIExist.txt");
String filePath = f.getPath();
boolean exist = f.isFile();
println(filePath, exist);
3 Likes
It works in java mode. Thanks!! By the way, could you please tell me how to achieve that in python mode if possible?
Or is there any function to get current processing data folder’s path?
You can do that in python like this:
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.
1 Like
f = this.dataFile('DoIExist.txt')
filePath, exist = f.path, f.isFile()
print filePath, exist
2 Likes
Yes! Just pass an empty string ""
to dataPath():
dataFolderPath = this.dataPath('') + '/'
print dataFolderPath
… or to dataFile():
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=,”:
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
2 Likes