How do I check if a file or directory exists in EK9?
← File I/O · Ref: Q153
EK9 provides the FileSystemPath type for working with file system paths. It supports checking existence, distinguishing files from directories, and performing path operations.
FILESYSTEMPATH TYPE
Create a FileSystemPath from a string:
path <- FileSystemPath("/home/user/data.txt")
FileSystemPath represents a location on the file system without opening or reading the file.
CHECKING EXISTENCE
Use exists() to check if the path points to something on disk:
if path.exists() stdout.println("Path exists")
The exists() method returns a Boolean that is set to true or false.
FILE VS DIRECTORY
Distinguish between files and directories:
if path.isFile() stdout.println("It is a file") else if path.isDirectory() stdout.println("It is a directory")
You can also check isReadable(), isWritable(), and isExecutable().
PATH OPERATIONS
FileSystemPath supports several path manipulation methods:
abs <- path.absolutePath() name <- path.fileName() parent <- path.parent() joined <- path + "subdir"
Use startsWith() and endsWith() for prefix and suffix checks.
See Q42 for the Path type. See Q151 for reading files. See Q152 for writing files.
Example
#!ek9 defines module qa.fileio.path defines program FilePathExistsDemo() stdout <- Stdout() // === CREATING FILESYSTEMPATH === path <- FileSystemPath("/tmp") stdout.println("Path: " + $path) // === CHECKING EXISTENCE === stdout.println("Exists: " + $path.exists()) // === FILE VS DIRECTORY === stdout.println("Is file: " + $path.isFile()) stdout.println("Is directory: " + $path.isDirectory()) // === PATH PROPERTIES === stdout.println("Is readable: " + $path.isReadable()) stdout.println("Is writable: " + $path.isWritable()) stdout.println("Is absolute: " + $path.isAbsolute()) // === PATH OPERATIONS === absolutePath <- path.absolutePath() stdout.println("Absolute: " + $absolutePath) // Path concatenation with + operator subPath <- path + "subdir" stdout.println("Sub path: " + $subPath)
Common mistakes
E50060 — FileSystemPath constructor expects a String argument. Passing an Integer triggers E50060 — constructor not resolved because no FileSystemPath(Integer) exists. File paths are always represented as strings in EK9. See ek9 -h E50060 for details.
Incorrect:
path <- FileSystemPath(42)
Correct:
path <- FileSystemPath("/tmp")
E50060 — FileSystemPath has no fileExists() method. Use exists() to check if the path points to something on disk. See ek9 -h E50060 for details.
Incorrect:
stdout.println("Exists: " + $path.fileExists())
Correct:
stdout.println("Exists: " + $path.exists())
E50060 — FileSystemPath has no getAbsolutePath() method. Use absolutePath() to get the absolute path. EK9 uses descriptive method names rather than Java-style getters. See ek9 -h E50060 for details.
Incorrect:
absolutePath <- path.getAbsolutePath()
Correct:
absolutePath <- path.absolutePath()
Other ways to ask this
- How do I use FileSystemPath in EK9?
- How do I check if a path exists in EK9?
- How do I work with file paths in EK9?
Coming from another language?
Java: Files.exists(Path) and Path for path operations. Python: os.path.exists() or pathlib.Path. Rust: Path::exists() and std::path::Path. Go: os.Stat() for existence checks. EK9: FileSystemPath('/path') with exists(), isFile(), isDirectory() methods and + operator for path joining.
Keywords: check, directory, exists, file, filesystem, folder, isDirectory, isFile, path, read, write