apache/hadoop · error · PathIsNotDirectoryException

Is not a directory

Error message

Is not a directory

What it means

PathData.checkIfExists (PathData.java:228) throws PathIsNotDirectoryException ('Is not a directory') when the path exists, a SHOULD_BE_DIRECTORY requirement was requested, but stat.isDirectory() is false. All three in-class callers — getDirectoryContents(), getDirectoryContentsIterator(), getPathDataForChild() — pass SHOULD_BE_DIRECTORY, i.e. any shell operation that must read the path as a directory.

Source

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

  protected enum FileTypeRequirement {
    SHOULD_NOT_BE_DIRECTORY, SHOULD_BE_DIRECTORY
  };

  /**
   * 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

View on GitHub (pinned to 2add963021)

Solutions

  1. Inspect the offending path: 'hadoop fs -ls <path>' — if it is a file, move or delete it
  2. Correct the argument so it names a directory (add the missing subdirectory component)
  3. In code, guard with 'if (!pd.stat.isDirectory())' or use fs.getFileStatus(path).isDirectory() before calling directory APIs

Example fix

# before
hadoop fs -cp /src/* /out    # /out exists as a FILE -> Is not a directory

# after
hadoop fs -mv /out /out.file.bak
hadoop fs -mkdir /out
hadoop fs -cp /src/* /out/
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

boolean isUsableDir(FileSystem fs, Path p) throws IOException {
  FileStatus st = fs.getFileStatus(p);
  return st.isDirectory();
}

Try / catch

catch (PathIsNotDirectoryException e) { relocate the conflicting file, mkdir the directory, then retry the operation }

Prevention

When it happens

Trigger: 'hadoop fs -ls /some/file' where the target exists as a regular file (ls of a bare file is fine via a different path, but directory-content listing hits this); a copy/move destination that exists as a file when getPathDataForChildPath tries to resolve children under it; passing a file where the API requires a directory source to enumerate.

Common situations: Destination path collision where a previous job left a file where a directory is expected; misconfigured output paths pointing at files; scripts assuming a path is a directory because a prior step 'should' have created it as one.

Related errors


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