expanduser

expanduser(path::AbstractString) -> AbstractString

On Unix systems, replace a tilde character at the start of a path with the current user's home directory.

Examples

  1. Expand tilde in a file path:

    julia> expanduser("~/Documents/file.txt")
    "/home/username/Documents/file.txt"

    This example expands the tilde character ~ at the beginning of the file path with the current user's home directory path.

  2. Expand tilde in a directory path:

    julia> expanduser("~/Downloads")
    "/home/username/Downloads"

    It expands the tilde character ~ at the start of the directory path to the user's home directory.

  3. Handle paths without tilde:
    julia> expanduser("/var/www/html/index.html")
    "/var/www/html/index.html"

    If the path does not contain a tilde character, the expanduser function returns the path as-is.

Common mistake example:

julia> expanduser("~username/Documents/file.txt")
ERROR: ArgumentError: invalid path: path must start with '~'

In this example, the path provided starts with ~username, which is not a valid usage of expanduser. The tilde character is expected to be at the start of the path, followed by a slash or the end of the string. Make sure to only use ~ at the start of the path to expand the user's home directory.

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.

*Required Field
Details

Checking you are not a robot: