FilePathsBase.jl provides a type based approach to working with filesystem paths in julia.
FilePathsBase.jl is registered, so you can to use Pkg.add
to install it.
julia> Pkg.add("FilePathsBase")
julia> using FilePathsBase
The first important difference about working with paths in FilePathsBase.jl is that path segments are represented as an immutable tuple of strings.
Path creation:
julia> Path("~/repos/FilePathsBase.jl/")
Paths.PosixPath(("~","repos","FilePathsBase.jl",""))
or
julia> p"~/repos/FilePathsBase.jl/"
Paths.PosixPath(("~","repos","FilePathsBase.jl",""))
Human readable file status info:
julia> stat(p"README.md")
Status(
device = 16777220,
inode = 48428965,
mode = -rw-r--r--,
nlink = 1,
uid = 501,
gid = 20,
rdev = 0,
size = 1880 (1.8K),
blksize = 4096 (4.0K),
blocks = 8,
mtime = 2016-02-16T00:49:27,
ctime = 2016-02-16T00:49:27,
)
Working with permissions:
julia> m = mode(p"README.md")
-rw-r--r--
julia> m - readable(:ALL)
--w-------
julia> m + executable(:ALL)
-rwxr-xr-x
julia> chmod(p"README.md", "+x")
julia> mode(p"README.md")
-rwxr-xr-x
julia> chmod(p"README.md", m)
julia> m = mode(p"README.md")
-rw-r--r--
julia> chmod(p"README.md", user=(READ+WRITE+EXEC), group=(READ+WRITE), other=READ)
julia> mode(p"README.md")
-rwxrw-r--
Reading and writing directly to file paths:
julia> write(p"testfile", "foobar")
6
julia> read(p"testfile")
"foobar"
All the standard methods for working with paths in base julia exist in the FilePathsBase.jl. The following describes the rough mapping of method names. Use ?
at the REPL to get the documentation and arguments as they may be different than the base implementations.
Base | FilePathsBase.jl |
---|---|
"/home/user/docs" | p"/home/user/docs" |
N/A | Path() |
pwd() | pwd(::Type{<:AbstractPath}) (or cwd()) |
homedir() | homedir(::Type{<:AbstractPath}) (or home()) |
cd() | cd() |
joinpath() | joinpath(), join, / |
basename() | basename() |
N/A | hasparent, parents, parent |
splitext | splitext |
N/A | filename |
N/A | extension |
N/A | extensions |
ispath | exists |
realpath | real |
normpath | norm |
abspath | abs |
relpath | relative |
stat | stat |
lstat | lstat |
filemode | mode |
mtime | modified |
ctime | created |
isdir | isdir |
isfile | isfile |
islink | islink |
issocket | issocket |
isfifo | isfifo |
ischardev | ischardev |
isblockdev | isblockdev |
isexecutable (deprecated) | isexecutable |
iswritable (deprecated) | iswritable |
isreadable (deprecated) | isreadable |
ismount | ismount |
isabspath | isabs |
splitdrive()[1] | drive |
N/A | root (property) |
split(p, "/") | segments (property) |
expanduser | expanduser |
mkdir | mkdir |
mkpath | N/A (use mkdir) |
symlink | symlink |
cp | cp |
mv | mv |
download | download |
readdir | readdir |
N/A | readpath |
N/A | walkpath |
rm | rm |
touch | touch |
tempname() | tempname(::Type{<:AbstractPath}) (or tmpname) |
tempdir() | tempdir(::Type{<:AbstractPath}) (or tmpdir) |
mktemp() | mktemp(::Type{<:AbstractPath}) (or mktmp) |
mktempdir() | mktempdir(::Type{<:AbstractPath}) (or mktmpdir) |
chmod | chmod (recursive unix-only) |
chown (unix only) | chown (unix only) |
read | read |
write | write |
@DIR | @PATH |
@FILE | @FILEPATH |