dirname
dirname(path::AbstractString) -> AbstractString
Get the directory part of a path.
Examples
-
Get the directory part of a path:
julia> dirname("/path/to/file.txt") "/path/to"
This example returns the directory part of the given path.
-
Handle paths without a directory component:
julia> dirname("file.txt") "."
If the path does not have a directory component, the function returns
"."
which represents the current directory. - Handle Windows-style paths:
julia> dirname("C:\\Users\\username\\Documents\\file.txt") "C:\\Users\\username\\Documents"
The function correctly handles Windows-style paths with backslashes.
Common mistake example:
julia> dirname("/path/to/directory/")
"/path/to/directory"
In this example, the path ends with a trailing slash. The dirname
function does not remove the trailing slash automatically. It only returns the directory part as-is.
See Also
abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.