apache/hadoop · error · PathIsDirectoryException

Is a directory

Error message

Is a directory

What it means

PathData.checkIfExists (PathData.java:231) throws PathIsDirectoryException ('Is a directory') when the path exists, the SHOULD_NOT_BE_DIRECTORY requirement was requested, but stat.isDirectory() is true. Note: in this tree the enum literal is defined and checked, but every current in-class caller (getDirectoryContents, getDirectoryContentsIterator, getPathDataForChild) passes SHOULD_BE_DIRECTORY, so this specific line is a defensive/legacy branch kept for callers that require a plain file.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/PathData.java:231

  /**
   * Ensure that the file exists and if it is or is not a directory
   * @param typeRequirement Set it to the desired requirement.
   * @throws PathIOException if file doesn't exist or the type does not match
   * what was specified in typeRequirement.
   */
  private void checkIfExists(FileTypeRequirement typeRequirement) 
  throws PathIOException {
    if (!exists) {
      throw new PathNotFoundException(toString());      
    }

    if ((typeRequirement == FileTypeRequirement.SHOULD_BE_DIRECTORY)
       && !stat.isDirectory()) {
      throw new PathIsNotDirectoryException(toString());
    } else if ((typeRequirement == FileTypeRequirement.SHOULD_NOT_BE_DIRECTORY)
              && stat.isDirectory()) {
      throw new PathIsDirectoryException(toString());
    }
  }
  
  /**
   * Returns a new PathData with the given extension.
   * @param extension for the suffix
   * @return PathData
   * @throws IOException shouldn't happen
   */
  public PathData suffix(String extension) throws IOException {
    return new PathData(fs, this+extension);
  }

  /**
   * Test if the parent directory exists
   * @return boolean indicating parent exists
   * @throws IOException upon unexpected error
   */

View on GitHub (pinned to 2add963021)

Solutions

  1. Point the command at a file inside the directory, or use the directory-appropriate command (e.g. -ls, or -get for the whole dir)
  2. Verify type before the call: 'hadoop fs -test -d <path>' and branch on it
  3. In code, guard with FileStatus.isDirectory() before file-only operations

Example fix

# before
hadoop fs -cat /data/logs       # /data/logs is a directory -> Is a directory

# after
hadoop fs -ls /data/logs        # list, or fetch a specific file:
hadoop fs -cat /data/logs/app.log
Defensive patterns

Strategy: type-guard

Validate before calling

FileStatus st = fs.getFileStatus(p);
if (st.isDirectory()) {
  throw new IllegalStateException("expected plain file: " + p);
}

Type guard

boolean isPlainFile(FileSystem fs, Path p) throws IOException {
  FileStatus st = fs.getFileStatus(p);
  return st.isFile() && !st.isSymlink();
}

Try / catch

catch (PathIsDirectoryException e) { target a specific file inside the directory or switch to a directory-appropriate command }

Prevention

When it happens

Trigger: A (present or future) caller invoking checkIfExists(SHOULD_NOT_BE_DIRECTORY) on a path that turns out to be a directory — the shape used by file-oriented commands (cat, checksum, tail, chmod-on-file) that must refuse directories. The user-visible 'Is a directory' failures from shell commands come from this exception family (PathIsDirectoryException is also thrown directly by FileSystem/FileContext-consuming code).

Common situations: 'hadoop fs -cat /some/dir' style operations on a directory; passing a directory to an API whose implementation requires a regular file (e.g., opening an InputStream); paths that used to be files and were replaced by directories between runs.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/b2435b60b926d222. Report an issue: GitHub.