{"record":{"id":"0769c8e5ec00d548","repo":"apache/hadoop","slug":"cannot-append-to-a-diretory-f","errorCode":null,"errorMessage":"Cannot append to a diretory (=\" + f + \" )","messagePattern":"Cannot append to a diretory \\(=\" \\+ f \\+ \" \\)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":622,"sourceCode":"      case StreamCapabilities.IOSTATISTICS_CONTEXT:\n        return true;\n      default:\n        return StoreImplementationUtils.isProbeForSyncable(capability);\n      }\n    }\n\n    @Override\n    public IOStatistics getIOStatistics() {\n      return ioStatistics;\n    }\n  }\n\n  @Override\n  public FSDataOutputStream append(Path f, int bufferSize,\n      Progressable progress) throws IOException {\n    FileStatus status = getFileStatus(f);\n    if (status.isDirectory()) {\n      throw new IOException(\"Cannot append to a diretory (=\" + f + \" )\");\n    }\n    return new FSDataOutputStream(new BufferedOutputStream(\n        createOutputStreamWithMode(f, true, null), bufferSize), statistics,\n        status.getLen());\n  }\n\n  @Override\n  public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize,\n    short replication, long blockSize, Progressable progress)\n    throws IOException {\n    return create(f, overwrite, true, bufferSize, replication, blockSize,\n        progress, null);\n  }\n\n  private FSDataOutputStream create(Path f, boolean overwrite,\n      boolean createParent, int bufferSize, short replication, long blockSize,\n      Progressable progress, FsPermission permission) throws IOException {\n    if (exists(f) && !overwrite) {","sourceCodeStart":604,"sourceCodeEnd":640,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L604-L640","documentation":"RawLocalFileSystem.append(Path, int, Progressable) first calls getFileStatus and throws a plain IOException when the target is a directory, because appending bytes to a directory is meaningless on a local filesystem. Note the historic typo 'diretory' in the message itself; match on exception type and prefix, not the exact spelling, if you must string-match.","triggerScenarios":"Calling fs.append(path) on a local Path that resolves to a directory (e.g. the job output directory instead of a part-file inside it), appending to a path that a prior mkdirs created, or a trailing-slash path that normalizes to a directory.","commonSituations":"Building an output path like /out instead of /out/part-00000, reusing a MapReduce output directory as an append target, configuration pointing fs.defaultFS at file:/// with a directory-valued path, or a symlink that resolves to a directory.","solutions":["Append to a file inside the directory: append(new Path(dir, \"part-00000\")) rather than the directory itself.","Check first: if (fs.getFileStatus(p).isDirectory()) { throw or pick a file path; } before calling append.","If you expected a file at that path, inspect the directory for stray mkdirs calls or a previous run that created a directory there, and delete/rename it.","Use fs.exists(p) && fs.getFileStatus(p).isFile() as a precondition when the path type is not guaranteed."],"exampleFix":"// before\nFileSystem fs = FileSystem.getLocal(conf);\nFSDataOutputStream out = fs.append(new Path(\"/jobs/out\")); // /jobs/out is a dir\n\n// after\nPath outDir = new Path(\"/jobs/out\");\nPath partFile = new Path(outDir, \"part-00000\");\nFSDataOutputStream out = fs.append(partFile);","handlingStrategy":"validation","validationCode":"public static FSDataOutputStream appendFile(FileSystem fs, Path p) throws IOException {\n  FileStatus st = fs.getFileStatus(p);\n  if (st.isDirectory()) {\n    throw new IllegalArgumentException(p + \" is a directory; append to a file inside it\");\n  }\n  return fs.append(p);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return fs.append(p);\n} catch (IOException e) {\n  if (fs.exists(p) && fs.getFileStatus(p).isDirectory()) {\n    throw new IllegalArgumentException(p + \" is a directory\", e);\n  }\n  throw e;\n}","preventionTips":["Construct append targets as dir + explicit filename, never the directory itself.","Stat and assert isFile() before append when the path type is external input.","Watch for trailing slashes and symlinks that normalize to directories."],"tags":["hadoop","local-filesystem","append","directory","ioexception"],"backgroundTag":"append-to-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}