{"record":{"id":"407335c7526da516","repo":"stanfordnlp/CoreNLP","slug":"could-not-create-directory-tgtdir-getabsolutepath","errorCode":null,"errorMessage":"Could not create directory <tgtDir.getAbsolutePath()>, as a file already exists at that path.","messagePattern":"Could not create directory <tgtDir\\.getAbsolutePath\\(\\)>, as a file already exists at that path\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/io/IOUtils.java","lineNumber":1742,"sourceCode":"      return new File(\"/\"+machineName+\"/scr1/\"+username);\n    } catch (Exception e) {\n      return new File(\"./scr/\"); // default scratch\n    }\n  }\n\n  /**\n   * Given a filepath, makes sure a directory exists there.  If not, creates and returns it.\n   * Same as ENSURE-DIRECTORY in CL.\n   *\n   * @param tgtDir The directory that you wish to ensure exists\n   * @throws IOException If directory can't be created, is an existing file, or for other reasons\n   */\n  public static File ensureDir(File tgtDir) throws IOException {\n    if (tgtDir.exists()) {\n      if (tgtDir.isDirectory()) {\n        return tgtDir;\n      } else {\n        throw new IOException(\"Could not create directory \"+tgtDir.getAbsolutePath()+\", as a file already exists at that path.\");\n      }\n    } else {\n      if ( ! tgtDir.mkdirs()) {\n        throw new IOException(\"Could not create directory \"+tgtDir.getAbsolutePath());\n      }\n      return tgtDir;\n    }\n  }\n\n  /**\n   * Given a filepath, delete all files in the directory recursively\n   * @param dir Directory from which to delete files\n   * @return {@code true} if the deletion is successful, {@code false} otherwise\n   */\n  public static boolean deleteDirRecursively(File dir) {\n    if (dir.isDirectory()) {\n      for (File f : dir.listFiles()) {\n        boolean success = deleteDirRecursively(f);","sourceCodeStart":1724,"sourceCodeEnd":1760,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/io/IOUtils.java#L1724-L1760","documentation":"IOUtils.ensureDir(File) throws this IOException when the target path already exists but is a regular file (or other non-directory), so the directory cannot be created there. The message includes the absolute path of the conflicting file.","triggerScenarios":"Calling IOUtils.ensureDir(tgtDir) where tgtDir.exists() is true and tgtDir.isDirectory() is false — i.e. a regular file, symlink-to-file, or special file occupies the intended directory path.","commonSituations":"A previous run created the path as a file (e.g. a lockfile or a mistakenly written output file); a config value for a directory path accidentally points at a file; a symlink points to a file; Docker bind-mounts creating a file where a directory was expected.","solutions":["Delete or rename the existing file at that path, or choose a different directory path, then call ensureDir again.","Before calling, check existence and kind: if the path exists and is not a directory, fail early with a clear message.","Fix the configuration/argument that supplies the path so it refers to the intended directory location."],"exampleFix":"// before\nIOUtils.ensureDir(new File(outputPath));\n// after\nFile dir = new File(outputPath);\nif (dir.exists() && !dir.isDirectory()) {\n  throw new IllegalStateException(\"Path is a file, expected directory: \" + dir.getAbsolutePath());\n}\nIOUtils.ensureDir(dir);","handlingStrategy":"validation","validationCode":"File dir = new File(path);\nif (dir.exists() && !dir.isDirectory())\n  throw new IllegalStateException(\"Path exists but is not a directory: \" + dir.getAbsolutePath());","typeGuard":null,"tryCatchPattern":"try {\n  IOUtils.ensureDir(dir);\n} catch (IOException e) {\n  if (new File(path).isFile()) {\n    throw new IllegalStateException(\"A file occupies the directory path: \" + path);\n  }\n  throw e;\n}","preventionTips":["Check path kind (file vs directory) before ensureDir.","Keep lock/temp files out of directory-path locations.","Beware symlinks: isDirectory() follows them."],"tags":["io","filesystem","directory","java"],"backgroundTag":"file-already-exists","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}