Very basic file reading question - where is the data directory?

I am trying to figure out how to access files. All the documentation I’ve seen says that loadImage() etc will only work on files in the data directory. (It seems very strange, that I can’t give an absolute pathname… but hey, ok.)

But…where is the data directory? I set up a test sketch and before I named it, it lived in a very long named place which would be super awkward to access for popping input files into it.

So I named it something sensible by doing Save. Now I can find its path by doing Sketch/ShowSketchFolder, and it looks like this:

But there’s no data directory in it. I’ve been looking at the Reference documentation and Tutorials and stack overflow and this forum for a very long time (…a few hours) and I am totally stuck. If input can only come from a data directory - where is this directory? Do I need to create it for each sketch?

This must be very simple. Can someone advise?

It’s automatically created when we drop a media type file onto the PDE.

We can access it via methods dataPath() and dataFile():

1 Like

Yeah, the data folder…

You can create it by hand in your Sketch folder

Each Sketch has its own data folder to store images etc.

See reference for dataPath() and sketchPath()

2 Likes

Thanks for the help.

When you talk about “drop a media type file onto the PDE” - I don’t understand what you mean. What kind of operation is this? Is it an “outside Processing” operation? Do you literally mean drag and drop a file while looking at the sketch directory in Finder (I’m on a Mac)? Or do you drop a file onto the code editing window (rather strange… but… )? etc

Thanks also for the help. When you say I “can” create it by hand - do you mean I “must” create it by hand? Is that how it’s intended to work?

What’s confusing me is that the documentation says that each Sketch “has” its own data folder. But I don’t see one when I look at a sketch in its containing folder. It doesn’t seem to “have” it. And it’s not obvious how to create it - it’s not obvious whether that’s something you do inside Processing somehow, or just in the normal file system (which seems clunky), or as the other poster suggested, by somehow “dropping” the file somewhere, somehow.

I think it’s one of those things that when you see it once it seems obvious afterwards.

Yes

You can also make the folder by hand and copy into it what you need

2 Likes

Indeed, the data folder is not there automatically.

You can create it by drag and drop an image or mp3 (I guess) onto the IDE / editor.

(Like you can drop a jar file to generate the code folder. Never mind.)

But you can create the data folder also by hand.

Besides: hit ctrl-k (in processing, the IDE or PDE, the Editor, all the same thing) to open the Sketch folder. Simple but I use it all the time.

Chrisir

3 Likes

I think it works in Sketch folder itself too but that’s kind of a bug or feature. Better use the data folder.

Just found this::::

A sketch folder sometimes contains other folders for media files and other code. When a font or image is added to a sketch by selecting “Add File…” from the Sketch menu, a “data” folder is created. Files may also be added to your Processing sketch by dragging them into the text editor. Image and sound files dragged into the application window will automatically be added to the current sketch’s “data” folder. All images, fonts, sounds, and other data files loaded in the sketch must be in this folder.

from https://processing.org/reference/environment/#Sketchbook

Of course you can! Try it with… dunno on an mac, on win it’s just c://hello//…

Besides look at selectFolder command in the reference to get a dialog box to select a folder in your Sketch (while your Sketch runs, that’s not the IDE)

1 Like
  • By default Processing looks for a filename at the sketchPath() 1st.
  • Only if it doesn’t find it there it looks at dataPath().
  • By default Processing saves everything on the sketchPath().
  • Wrap a filename on dataPath() in order to save it on subfolder “data/”.

An example sketch using dataFile() + listPaths():

2 Likes

See also here Adding images with absolute paths

1 Like

To figure out this issue I read the processing source.
Anywhere in processing the string:
sketchPath
exists.
Since the variable is private use sketchPath(), here is the source:



  static protected String calcSketchPath() {
    // try to get the user folder. if running under java web start,
    // this may cause a security exception if the code is not signed.
    // http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1159386274
    String folder = null;
    try {
      folder = System.getProperty("user.dir");

      URL jarURL =
          PApplet.class.getProtectionDomain().getCodeSource().getLocation();
      // Decode URL
      String jarPath = jarURL.toURI().getSchemeSpecificPart();

      // Workaround for bug in Java for OS X from Oracle (7u51)
      // https://github.com/processing/processing/issues/2181
      if (platform == MACOS) {
        if (jarPath.contains("Contents/Java/")) {
          String appPath = jarPath.substring(0, jarPath.indexOf(".app") + 4);
          File containingFolder = new File(appPath).getParentFile();
          folder = containingFolder.getAbsolutePath();
        }
      } else {
        // Working directory may not be set properly, try some options
        // https://github.com/processing/processing/issues/2195
        if (jarPath.contains("/lib/")) {
          // Windows or Linux, back up a directory to get the executable
          folder = new File(jarPath, "../..").getCanonicalPath();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return folder;
  }


  public String sketchPath() {
    if (sketchPath == null) {
      sketchPath = calcSketchPath(); // this line if sketchPath hasn't been initialised
    }
    return sketchPath;
  }

Now, the data directory can be located simply by adding “/data” to the sketch path and there you go!
Using this code (reference on line 7673 PApplet.java)
you can get the sketch path (java.io.File may be required)

void setup() {
  println(sketchPath() /*path of the sketch*/ + File.separator /*probably slash*/ + "data" + File.separator);
}

And this piece works fine for me. Logs the data directory even if its an unsaved program and with no libraries (java.io.File not included).

Tested on Ubuntu 18.04

1 Like

It appears you are using iCloud Drive, try saving it to your local disk instead.