FilePathsBase.jl
FilePathsBase.jl provides a type based approach to working with filesystem paths in julia.
Intallation
FilePathsBase.jl is registered, so you can to use Pkg.add to install it.
julia> Pkg.add("FilePathsBase")Usage
julia> using FilePathsBaseThe 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/")
p"~/repos/FilePathsBase.jl/"or
julia> p"~/repos/FilePathsBase.jl/"
p"~/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"API
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() | cwd() |
| homedir() | home() |
| cd() | cd() |
| joinpath() | joinpath(), join, / |
| basename() | basename() |
| N/A | hasparent, parents, parent |
| splitext(basename())[1] | filename |
| splitext(basename())[2] | 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 | copy |
| mv | move |
| readdir | readdir |
| N/A | readpath |
| N/A | walkpath |
| rm | remove |
| touch | touch |
| tempname | tmpname |
| tempdir | tmpdir |
| mktemp | mktmp |
| mktempdir | mktmpdir |
| chmod | chmod (recursive unix-only) |
| chown (unix only) | chown (unix only) |
| read | read |
| write | write |
| @DIR | @PATH |
| @FILE | @FILEPATH |
FilePathsBase.AbstractPath — Type.AbstractPathDefines an abstract filesystem path.
Properties
segments::Tuple{Vararg{String}}- path segments (required)root::String- path root (defaults to "/")drive::String- path drive (defaults to "")separator::String- path separator (defaults to "/")
Required Methods
T(str::String)- A string constructorFilePathsBase.ispathtype(::Type{T}, x::AbstractString) = trueread(path::T)write(path::T, data)exists(path::T- whether the path existsstat(path::T)- File status describing permissions, size and creation/modified timesmkdir(path::T; kwargs...)- Create a new directoryrm(path::T; kwags...)- Remove a file or directoryreaddir(path::T)- Scan all files and directories at a specific path level
FilePathsBase.Path — Function.Path() -> SystemPath
Path(fp::Tuple) -> SystemPath
Path(fp::P) where P <: AbstractPath) -> P
Path(fp::AbstractString) -> AbstractPath
Path(fp::P, segments::Tuple) -> PResponsible for creating the appropriate platform specific path (e.g., PosixPath and WindowsPath` for Unix and Windows systems respectively)
NOTE: Path(::AbstractString) can also work for custom paths if ispathtype is defined for that type.
FilePathsBase.SystemPath — Constant.SystemPathA union of PosixPath and WindowsPath which is used for writing methods that wrap base functionality.
FilePathsBase.PosixPath — Type.PosixPath()
PosixPath(str)Represents any posix path (e.g., /home/user/docs)
FilePathsBase.WindowsPath — Type.WindowsPath()
WindowsPath(str)Represents a windows path (e.g., C:\User\Documents)
FilePathsBase.Mode — Type.Mode(m::UInt8)
Mode(;user::UInt8=0o0, group::UInt8=0o0, other::UInt8=0o0)
Mode(mode::UInt8, usr_grps::Symbol...)
Mode(str)Provides an abstraction for working with posix file permissions. A lot of the low level permissions code for this type was below and the corresponding constants have been translated from cpython's Lib/stat.py.
Examples
julia> Mode("-rwxr-x--x")
-rwxr-x--xFilePathsBase.@p_str — Macro.@p_str -> PathConstructs a Path (platform specific subtype of AbstractPath), such as p"~/.juliarc.jl".
FilePathsBase.@__PATH__ — Macro.@__PATH__ -> SystemPath@PATH expands to a path with the directory part of the absolute path of the file containing the macro. Returns an empty Path if run from a REPL or if evaluated by julia -e <expr>.
FilePathsBase.@__FILEPATH__ — Macro.@__FILEPATH__ -> SystemPath@FILEPATH expands to a path with the absolute file path of the file containing the macro. Returns an empty Path if run from a REPL or if evaluated by julia -e <expr>.
FilePathsBase.@LOCAL — Macro.@LOCAL(filespec)Construct an absolute path to filespec relative to the source file containing the macro call.
FilePathsBase.cwd — Function. cwd() -> SystemPathGet the current working directory.
Examples
julia> cwd()
p"/home/JuliaUser"
julia> cd(p"/home/JuliaUser/Projects/julia")
julia> cwd()
p"/home/JuliaUser/Projects/julia"Missing docstring for FilePathsBase.home. Check Documenter's build log for details.
FilePathsBase.hasparent — Function.hasparent(fp::AbstractPath) -> BoolReturns whether there is a parent directory component to the supplied path.
FilePathsBase.parents — Function.parents{T<:AbstractPath}(fp::T) -> Array{T}Return all parents of the path. If no parent exists then either "/" or "." will be returned depending on whether the path is absolute.
Example
``` julia> parents(p"~/.julia/v0.6/REQUIRE") 3-element Array{FilePathsBase.PosixPath,1}: p"~" p"~/.julia" p"~/.julia/v0.6"
julia> parents(p"/etc") 1-element Array{PosixPath,1}: p"/"
julia> parents(p"etc") 1-element Array{PosixPath,1}: p"."
julia> parents(p".") 1-element Array{PosixPath,1}: p"." ```
Base.parent — Function.parent{T<:AbstractPath}(fp::T) -> TReturns the parent of the supplied path. If no parent exists then either "/" or "." will be returned depending on whether the path is absolute.
Example
julia> parent(p"~/.julia/v0.6/REQUIRE")
p"~/.julia/v0.6"
julia> parent(p"/etc")
p"/"
julia> parent(p"etc")
p"."
julia> parent(p".")
p"."Base.:* — Method.*(a::T, b::Union{T, AbstractString, AbstractChar}...) where {T <: AbstractPath} -> T
Concatenate paths, strings and/or characters, producing a new path. This is equivalent to concatenating the string representations of paths and other strings and then constructing a new path.
Example
julia> p"foo" * "bar"
p"foobar"Base.:/ — Method./(a::AbstractPath, b::Union{AbstractPath, AbstractString}...) -> AbstractPath
Join the path components into a new fulll path, equivalent to calling joinpath
Example
julia> p"foo" / "bar"
p"foo/bar"
julia> p"foo" / "bar" / "baz"
p"foo/bar/baz"Base.join — Method.join(root::AbstractPath, pieces::Union{AbstractPath, AbstractString}...) -> AbstractPathJoins path components into a full path.
Example
julia> join(p"~/.julia/v0.6", "REQUIRE")
p"~/.julia/v0.6/REQUIRE"FilePathsBase.filename — Method.filename(fp::AbstractPath) -> AbstractStringExtracts the basename without any extensions.
Example
julia> filename(p"~/repos/FilePathsBase.jl/src/FilePathsBase.jl")
"FilePathsBase"FilePathsBase.extension — Method.extension(fp::AbstractPath) -> AbstractStringExtracts the last extension from a filename if there any, otherwise it returns an empty string.
Example
julia> extension(p"~/repos/FilePathsBase.jl/src/FilePathsBase.jl")
"jl"FilePathsBase.extensions — Method.extensions(fp::AbstractPath) -> AbstractStringExtracts all extensions from a filename if there any, otherwise it returns an empty string.
Example
julia> extensions(p"~/repos/FilePathsBase.jl/src/FilePathsBase.jl.bak")
2-element Array{SubString{String},1}:
"jl"
"bak"Base.isempty — Method.isempty(fp::AbstractPath) -> BoolReturns whether or not a path is empty.
NOTE: Empty paths are usually only created by Path(), as p"" and Path("") will default to using the current directory (or p".").
LinearAlgebra.norm — Method.norm(fp::AbstractPath) -> AbstractPathNormalizes a path by removing "." and ".." entries.
Base.abs — Method.abs(fp::AbstractPath) -> AbstractPathCreates an absolute path by adding the current working directory if necessary.
Missing docstring for FilePathsBase.isabs(::AbstractPath). Check Documenter's build log for details.
FilePathsBase.relative — Method.relative{T<:AbstractPath}(fp::T, start::T=T("."))Creates a relative path from either the current directory or an arbitrary start directory.
Base.real — Method.real(path::AbstractPath) -> AbstractPathCanonicalize a path by expanding symbolic links and removing "." and ".." entries.
FilePathsBase.mode — Method.mode(fp::AbstractPath) -> ModeReturns the Mode for the specified path.
Example
julia> mode(p"src/FilePathsBase.jl")
-rw-r--r--FilePathsBase.modified — Method.modified(fp::AbstractPath) -> DateTimeReturns the last modified date for the path.
Example
julia> modified(p"src/FilePathsBase.jl")
2017-06-20T04:01:09FilePathsBase.created — Method.created(fp::AbstractPath) -> DateTimeReturns the creation date for the path.
Example
julia> created(p"src/FilePathsBase.jl")
2017-06-20T04:01:09FilePathsBase.isexecutable — Function.isexecutable(fp::SystemPath) -> BoolReturns whether the path is executable for the current user.
Base.iswritable — Method.iswritable(fp::AbstractPath) -> BoolReturns whether the path is writable for the current user.
Base.isreadable — Method.isreadable(fp::SystemPath) -> BoolReturns whether the path is readable for the current user.
Base.Filesystem.cp — Method.cp(src::AbstractPath, dst::AbstractPath; force=false, follow_symlinks=false)Copy the file or directory from src to dst. An existing dst will only be overwritten if force=true. If the path types support symlinks then follow_symlinks=true will copy the contents of the symlink to the destination.
Base.Filesystem.mv — Method.mv(src::AbstractPath, dst::AbstractPath; force=false)Move the file or director from src to dst. An exist dst will only be overwritten if force=true.
Base.download — Method.download(url::Union{AbstractString, AbstractPath}, localfile::AbstractPath)Download a file from the remote url and save it to the localfile path.
FilePathsBase.readpath — Function.readpath(fp::P) where {P <: AbstractPath} -> Vector{P}FilePathsBase.walkpath — Function.walkpath(fp::AbstractPath; topdown=true, follow_symlinks=false, onerror=throw)Performs a depth first search through the directory structure
Base.open — Method.open(filename::AbstractPath; keywords...) -> FileBuffer open(filename::AbstractPath, mode="r) -> FileBuffer
Return a default FileBuffer for open calls to paths which only support read and write methods. See base open docs for details on valid keywords.
Missing docstring for FilePathsBase.tmpname. Check Documenter's build log for details.
Missing docstring for FilePathsBase.tmpdir. Check Documenter's build log for details.
Missing docstring for FilePathsBase.mktmp. Check Documenter's build log for details.
Missing docstring for FilePathsBase.mktmpdir. Check Documenter's build log for details.
Base.Filesystem.chown — Method.chown(fp::SystemPath, user::AbstractString, group::AbstractString; recursive=false)Change the user and group of the fp.
Base.Filesystem.chmod — Method.chmod(fp::SystemPath, mode::Mode; recursive=false)
chmod(fp::SystemPath, mode::Integer; recursive=false)
chmod(fp::SystemPath, user::UIn8=0o0, group::UInt8=0o0, other::UInt8=0o0; recursive=false)
chmod(fp::SystemPath, symbolic_mode::AbstractString; recursive=false)Provides various methods for changing the mode of a fp.
Examples
julia> touch(p"newfile")
Base.Filesystem.File(false, RawFD(-1))
julia> mode(p"newfile")
-rw-r--r--
julia> chmod(p"newfile", 0o755)
julia> mode(p"newfile")
-rwxr-xr-x
julia> chmod(p"newfile", "-x")
julia> mode(p"newfile")
-rw-r--r--
julia> chmod(p"newfile", user=(READ+WRITE+EXEC), group=(READ+EXEC), other=READ)
julia> mode(p"newfile")
-rwxr-xr--
julia> chmod(p"newfile", mode(p"src/FilePathsBase.jl"))
julia> mode(p"newfile")
-rw-r--r--FilePathsBase.TestPaths — Module.TestPathsThis module is intended to be used for testing new path types to ensure that they are adhering to the AbstractPath API.
Example
# Create a PathSet
ps = PathSet(; symlink=true)
# Select the subset of tests to run
# Inspect TestPaths.TESTALL to see full list
testsets = [
test_constructor,
test_registration,
test_show,
test_parse,
test_convert,
test_components,
test_parents,
test_join,
test_basename,
test_filename,
test_extensions,
test_isempty,
test_norm,
test_real,
test_relative,
test_abs,
test_isdir,
test_isfile,
test_stat,
test_size,
test_modified,
test_created,
test_cd,
test_readpath,
test_walkpath,
test_read,
test_write,
test_mkdir,
test_cp,
test_mv,
test_sync,
test_symlink,
test_touch,
test_tmpname,
test_tmpdir,
test_mktmp,
test_mktmpdir,
test_download,
]
# Run all the tests
test(ps, testsets)FilePathsBase.TestPaths.PathSet — Type.PathSet(root::AbstractPath=tmpdir(); symlink=false)Constructs a common test path hierarchy to running shared API tests.
Hierarchy:
root
|-- foo
| |-- baz.txt
|-- bar
| |-- qux
| |-- quux.tar.gz
|-- fred
| |-- plugh