API
To compare and contrast FilePathsBase with Base.Filesystem we provide tables for common operations. Use ? at the REPL to get the documentation and arguments as they may be different than the base implementations.
Operations
A table of common operations with Filesystem and FilePathsBase.
| Filesystem | FilePathsBase.jl |
|---|---|
| "/home/user/docs" | p"/home/user/docs" |
| N/A | Path() |
| pwd() | pwd(SystemPath) or cwd() |
| homedir() | homedir(SystemPath) or home() |
| cd() | cd() |
| joinpath() | / |
| basename() | basename() |
| N/A | hasparent, parents, parent |
| splitext | splitext |
| N/A | filename |
| N/A | extension |
| N/A | extensions |
| ispath | exists |
| realpath | canonicalize |
| normpath | normalize |
| abspath | absolute |
| relpath | relative |
| stat | stat |
| lstat | lstat |
| filemode | mode |
| filesize | filesize |
| 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 | isabsolute |
| 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 |
| walkpath | 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 |
Aliases
A slightly reduced list of operations/aliases that will work with both strings and path types. The Filesystem and FilePathsBase columns indicate what type will be returned from each each library. As you'd expect, most return types match the input argument(s).
| Function Name | Filesystem | FilePathsBase |
|---|---|---|
| cd | AbstractString | AbstractPath |
| joinpath | AbstractString | AbstractPath |
| basename | AbstractString | AbstractString |
| splitext | (AbstractString, AbstractString) | (AbstractPath, AbstractString) |
| ispath | Bool | Bool |
| realpath | AbstractString | AbstractPath |
| normpath | AbstractString | AbstractPath |
| abspath | AbstractString | AbstractPath |
| relpath | AbstractString | AbstractPath |
| stat | StatStruct | FilePathsBase.Status |
| lstat | StatStruct | FilePathsBase.Status |
| filemode | UInt64 | FilePathsBase.Mode |
| filesize | Int64 | Int64 |
| mtime | Float64 | Float64 |
| ctime | Float64 | Float64 |
| isdir | Bool | Bool |
| isfile | Bool | Bool |
| islink | Bool | Bool |
| issocket | Bool | Bool |
| isfifo | Bool | Bool |
| ischardev | Bool | Bool |
| isblockdev | Bool | Bool |
| ismount | Bool | Bool |
| isabspath | Bool | Bool |
| expanduser | AbstractString | AbstractPath |
| mkdir | AbstractString | AbstractPath |
| mkpath | AbstractString | AbstractPath |
| symlink | Nothing | Nothing |
| cp | AbstractString | AbstractPath |
| mv | AbstractString | AbstractPath |
| download | AbstractString | AbstractPath |
| readdir | AbstractString | AbstractString |
| rm | Nothing | Nothing |
| touch | AbstractString | AbstractPath |
| chmod | AbstractString | AbstractPath |
| chown | AbstractString | AbstractPath |
| read(fp, T) | T | T |
| write | Int64 | Int64 |
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
tryparse(::Type{T}, str::String)- For parsing string representations of your pathread(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::AbstractString) -> AbstractPath
Path(fp::P; overrides...) -> PResponsible for creating the appropriate platform specific path (e.g., PosixPath and WindowsPath` for Unix and Windows systems respectively)
FilePathsBase.SystemPath — Type.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.
Example
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"FilePathsBase.:/ — Method./(a::AbstractPath, b::Union{AbstractPath, AbstractString}...) -> AbstractPath
Join the path components into a new full path, equivalent to calling joinpath.
Example
julia> p"foo" / "bar"
p"foo/bar"
julia> p"foo" / "bar" / "baz"
p"foo/bar/baz"FilePathsBase.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 the extension.
Example
julia> filename(p"~/repos/FilePathsBase.jl/src/FilePathsBase.jl")
"FilePathsBase"
julia> filename(p"~/Downloads/julia-1.4.0-linux-x86_64.tar.gz")
"julia-1.4.0-linux-x86_64.tar"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".").
FilePathsBase.normalize — Method.normalize(fp::AbstractPath) -> AbstractPathnormalizes a path by removing "." and ".." entries.
FilePathsBase.absolute — Method.absolute(fp::AbstractPath) -> AbstractPathCreates an absolute path by adding the current working directory if necessary.
FilePathsBase.isabsolute — Method.isabsolute(fp::AbstractPath) -> BoolReturns true if fp.root is not empty, indicating that it is an absolute path.
FilePathsBase.relative — Method.relative{T<:AbstractPath}(fp::T, start::T=cwd())Creates a relative path from either the current directory or an arbitrary start directory.
Missing docstring for Base.readlink(::AbstractPath). Check Documenter's build log for details.
FilePathsBase.canonicalize — Method.canonicalize(path::AbstractPath) -> AbstractPathCanonicalize a path by making it absolute, . or .. segments and resolving any symlinks if applicable.
WARNING: Fallback behaviour ignores symlinks and should be extended for paths where symlinks are permitted (e.g., SystemPaths).
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.
NOTE: Not downloading into a localfile directory matches the base Julia behaviour. https://github.com/rofinn/FilePathsBase.jl/issues/48
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_registration,
test_show,
test_cmd,
test_parse,
test_convert,
test_components,
test_parents,
test_join,
test_splitext,
test_basename,
test_filename,
test_extensions,
test_isempty,
test_normalize,
test_canonicalize,
test_relative,
test_absolute,
test_isdir,
test_isfile,
test_stat,
test_filesize,
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