文件系统

Base.Filesystem.pwd — Function.

  1. pwd() -> AbstractString

Get the current working directory.

Examples

  1. julia> pwd()
  2. "/home/JuliaUser"
  3. julia> cd("/home/JuliaUser/Projects/julia")
  4. julia> pwd()
  5. "/home/JuliaUser/Projects/julia"

source

Base.Filesystem.cd — Method.

  1. cd(dir::AbstractString=homedir())

Set the current working directory.

Examples

  1. julia> cd("/home/JuliaUser/Projects/julia")
  2. julia> pwd()
  3. "/home/JuliaUser/Projects/julia"
  4. julia> cd()
  5. julia> pwd()
  6. "/home/JuliaUser"

source

Base.Filesystem.cd — Method.

  1. cd(f::Function, dir::AbstractString=homedir())

Temporarily change the current working directory to dir, apply function f and finally return to the original directory.

Examples

  1. julia> pwd()
  2. "/home/JuliaUser"
  3. julia> cd(readdir, "/home/JuliaUser/Projects/julia")
  4. 34-element Array{String,1}:
  5. ".circleci"
  6. ".freebsdci.sh"
  7. ".git"
  8. ".gitattributes"
  9. ".github"
  10. "test"
  11. "ui"
  12. "usr"
  13. "usr-staging"
  14. julia> pwd()
  15. "/home/JuliaUser"

source

Base.Filesystem.readdir — Function.

  1. readdir(dir::AbstractString=".") -> Vector{String}

Return the files and directories in the directory dir (or the current working directory if not given).

Examples

  1. julia> readdir("/home/JuliaUser/Projects/julia")
  2. 34-element Array{String,1}:
  3. ".circleci"
  4. ".freebsdci.sh"
  5. ".git"
  6. ".gitattributes"
  7. ".github"
  8. "test"
  9. "ui"
  10. "usr"
  11. "usr-staging"

source

Base.Filesystem.walkdir — Function.

  1. walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)

Return an iterator that walks the directory tree of a directory. The iterator returns a tuple containing (rootpath, dirs, files). The directory tree can be traversed top-down or bottom-up. If walkdir encounters a SystemError it will rethrow the error by default. A custom error handling function can be provided through onerror keyword argument. onerror is called with a SystemError as argument.

Examples

  1. for (root, dirs, files) in walkdir(".")
  2. println("Directories in $root")
  3. for dir in dirs
  4. println(joinpath(root, dir)) # path to directories
  5. end
  6. println("Files in $root")
  7. for file in files
  8. println(joinpath(root, file)) # path to files
  9. end
  10. end
  1. julia> mkpath("my/test/dir");
  2. julia> itr = walkdir("my");
  3. julia> (root, dirs, files) = first(itr)
  4. ("my", ["test"], String[])
  5. julia> (root, dirs, files) = first(itr)
  6. ("my/test", ["dir"], String[])
  7. julia> (root, dirs, files) = first(itr)
  8. ("my/test/dir", String[], String[])

source

Base.Filesystem.mkdir — Function.

  1. mkdir(path::AbstractString; mode::Unsigned = 0o777)

Make a new directory with name path and permissions mode. mode defaults to 0o777, modified by the current file creation mask. This function never creates more than one directory. If the directory already exists, or some intermediate directories do not exist, this function throws an error. See mkpath for a function which creates all required intermediate directories. Return path.

Examples

  1. julia> mkdir("testingdir")
  2. "testingdir"
  3. julia> cd("testingdir")
  4. julia> pwd()
  5. "/home/JuliaUser/testingdir"

source

Base.Filesystem.mkpath — Function.

  1. mkpath(path::AbstractString; mode::Unsigned = 0o777)

Create all directories in the given path, with permissions mode. mode defaults to 0o777, modified by the current file creation mask. Return path.

Examples

  1. julia> mkdir("testingdir")
  2. "testingdir"
  3. julia> cd("testingdir")
  4. julia> pwd()
  5. "/home/JuliaUser/testingdir"
  6. julia> mkpath("my/test/dir")
  7. "my/test/dir"
  8. julia> readdir()
  9. 1-element Array{String,1}:
  10. "my"
  11. julia> cd("my")
  12. julia> readdir()
  13. 1-element Array{String,1}:
  14. "test"
  15. julia> readdir("test")
  16. 1-element Array{String,1}:
  17. "dir"

source

Base.Filesystem.symlink — Function.

  1. symlink(target::AbstractString, link::AbstractString)

Creates a symbolic link to target with the name link.

Note

This function raises an error under operating systems that do not support soft symbolic links, such as Windows XP.

source

Base.Filesystem.readlink — Function.

  1. readlink(path::AbstractString) -> AbstractString

Return the target location a symbolic link path points to.

source

Base.Filesystem.chmod — Function.

  1. chmod(path::AbstractString, mode::Integer; recursive::Bool=false)

Change the permissions mode of path to mode. Only integer modes (e.g. 0o777) are currently supported. If recursive=true and the path is a directory all permissions in that directory will be recursively changed. Return path.

source

Base.Filesystem.chown — Function.

  1. chown(path::AbstractString, owner::Integer, group::Integer=-1)

Change the owner and/or group of path to owner and/or group. If the value entered for owner or group is -1 the corresponding ID will not change. Only integer owners and groups are currently supported. Return path.

source

Base.Libc.RawFD — Type.

  1. RawFD

Primitive type which wraps the native OS file descriptor. RawFDs can be passed to methods like stat to discover information about the underlying file, and can also be used to open streams, with the RawFD describing the OS file backing the stream.

source

Base.stat — Function.

  1. stat(file)

Returns a structure whose fields contain information about the file. The fields of the structure are:

Name Description
size The size (in bytes) of the file
device ID of the device that contains the file
inode The inode number of the file
mode The protection mode of the file
nlink The number of hard links to the file
uid The user id of the owner of the file
gid The group id of the file owner
rdev If this file refers to a device, the ID of the device it refers to
blksize The file-system preferred block size for the file
blocks The number of such blocks allocated
mtime Unix timestamp of when the file was last modified
ctime Unix timestamp of when the file was created

source

Base.Filesystem.lstat — Function.

  1. lstat(file)

Like stat, but for symbolic links gets the info for the link itself rather than the file it refers to. This function must be called on a file path rather than a file object or a file descriptor.

source

Base.Filesystem.ctime — Function.

  1. ctime(file)

Equivalent to stat(file).ctime.

source

Base.Filesystem.mtime — Function.

  1. mtime(file)

Equivalent to stat(file).mtime.

source

Base.Filesystem.filemode — Function.

  1. filemode(file)

Equivalent to stat(file).mode.

source

Base.Filesystem.filesize — Function.

  1. filesize(path...)

Equivalent to stat(file).size.

source

Base.Filesystem.uperm — Function.

  1. uperm(file)

Get the permissions of the owner of the file as a bitfield of

Value Description
01 Execute Permission
02 Write Permission
04 Read Permission

For allowed arguments, see stat.

source

Base.Filesystem.gperm — Function.

  1. gperm(file)

Like uperm but gets the permissions of the group owning the file.

source

Base.Filesystem.operm — Function.

  1. operm(file)

Like uperm but gets the permissions for people who neither own the file nor are a member of the group owning the file

source

Base.Filesystem.cp — Function.

  1. cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false)

Copy the file, link, or directory from src to dest. force=true will first remove an existing dst.

If follow_symlinks=false, and src is a symbolic link, dst will be created as a symbolic link. If follow_symlinks=true and src is a symbolic link, dst will be a copy of the file or directory src refers to. Return dst.

source

Base.download — Function.

  1. download(url::AbstractString, [localfile::AbstractString])

Download a file from the given url, optionally renaming it to the given local file name. Note that this function relies on the availability of external tools such as curl, wget or fetch to download the file and is provided for convenience. For production use or situations in which more options are needed, please use a package that provides the desired functionality instead.

source

Base.Filesystem.mv — Function.

  1. mv(src::AbstractString, dst::AbstractString; force::Bool=false)

Move the file, link, or directory from src to dst. force=true will first remove an existing dst. Return dst.

Examples

  1. julia> write("hello.txt", "world");
  2. julia> mv("hello.txt", "goodbye.txt")
  3. "goodbye.txt"
  4. julia> "hello.txt" in readdir()
  5. false
  6. julia> readline("goodbye.txt")
  7. "world"
  8. julia> write("hello.txt", "world2");
  9. julia> mv("hello.txt", "goodbye.txt")
  10. ERROR: ArgumentError: 'goodbye.txt' exists. `force=true` is required to remove 'goodbye.txt' before moving.
  11. Stacktrace:
  12. [1] #checkfor_mv_cp_cptree#10(::Bool, ::Function, ::String, ::String, ::String) at ./file.jl:293
  13. [...]
  14. julia> mv("hello.txt", "goodbye.txt", force=true)
  15. "goodbye.txt"
  16. julia> rm("goodbye.txt");

source

Base.Filesystem.rm — Function.

  1. rm(path::AbstractString; force::Bool=false, recursive::Bool=false)

Delete the file, link, or empty directory at the given path. If force=true is passed, a non-existing path is not treated as error. If recursive=true is passed and the path is a directory, then all contents are removed recursively.

Examples

  1. julia> mkpath("my/test/dir");
  2. julia> rm("my", recursive=true)
  3. julia> rm("this_file_does_not_exist", force=true)
  4. julia> rm("this_file_does_not_exist")
  5. ERROR: IOError: unlink: no such file or directory (ENOENT)
  6. Stacktrace:
  7. [...]

source

Base.Filesystem.touch — Function.

  1. touch(path::AbstractString)

Update the last-modified timestamp on a file to the current time. Return path.

Examples

  1. julia> write("my_little_file", 2);
  2. julia> mtime("my_little_file")
  3. 1.5273815391135583e9
  4. julia> touch("my_little_file");
  5. julia> mtime("my_little_file")
  6. 1.527381559163435e9

We can see the mtime has been modified by touch.

source

Base.Filesystem.tempname — Function.

  1. tempname()

Generate a temporary file path. This function only returns a path; no file is created. The path is likely to be unique, but this cannot be guaranteed.

Warning

This can lead to race conditions if another process obtains the same file name and creates the file before you are able to. Using mktemp() is recommended instead.

source

Base.Filesystem.tempdir — Function.

  1. tempdir()

Obtain the path of a temporary directory (possibly shared with other processes).

source

Base.Filesystem.mktemp — Method.

  1. mktemp(parent=tempdir())

Return (path, io), where path is the path of a new temporary file in parent and io is an open file object for this path.

source

Base.Filesystem.mktemp — Method.

  1. mktemp(f::Function, parent=tempdir())

Apply the function f to the result of mktemp(parent) and remove the temporary file upon completion.

source

Base.Filesystem.mktempdir — Method.

  1. mktempdir(parent=tempdir())

Create a temporary directory in the parent directory and return its path. If parent does not exist, throw an error.

source

Base.Filesystem.mktempdir — Method.

  1. mktempdir(f::Function, parent=tempdir())

Apply the function f to the result of mktempdir(parent) and remove the temporary directory upon completion.

source

Base.Filesystem.isblockdev — Function.

  1. isblockdev(path) -> Bool

Return true if path is a block device, false otherwise.

source

Base.Filesystem.ischardev — Function.

  1. ischardev(path) -> Bool

Return true if path is a character device, false otherwise.

source

Base.Filesystem.isdir — Function.

  1. isdir(path) -> Bool

Return true if path is a directory, false otherwise.

Examples

  1. julia> isdir(homedir())
  2. true
  3. julia> isdir("not/a/directory")
  4. false

source

Base.Filesystem.isfifo — Function.

  1. isfifo(path) -> Bool

Return true if path is a FIFO, false otherwise.

source

Base.Filesystem.isfile — Function.

  1. isfile(path) -> Bool

Return true if path is a regular file, false otherwise.

Examples

  1. julia> isfile(homedir())
  2. false
  3. julia> f = open("test_file.txt", "w");
  4. julia> isfile(f)
  5. true
  6. julia> close(f); rm("test_file.txt")

source

Base.Filesystem.islink — Function.

  1. islink(path) -> Bool

Return true if path is a symbolic link, false otherwise.

source

Base.Filesystem.ismount — Function.

  1. ismount(path) -> Bool

Return true if path is a mount point, false otherwise.

source

Base.Filesystem.ispath — Function.

  1. ispath(path) -> Bool

Return true if path is a valid filesystem path, false otherwise.

source

Base.Filesystem.issetgid — Function.

  1. issetgid(path) -> Bool

Return true if path has the setgid flag set, false otherwise.

source

Base.Filesystem.issetuid — Function.

  1. issetuid(path) -> Bool

Return true if path has the setuid flag set, false otherwise.

source

Base.Filesystem.issocket — Function.

  1. issocket(path) -> Bool

Return true if path is a socket, false otherwise.

source

Base.Filesystem.issticky — Function.

  1. issticky(path) -> Bool

Return true if path has the sticky bit set, false otherwise.

source

Base.Filesystem.homedir — Function.

  1. homedir() -> AbstractString

Return the current user's home directory.

Note

homedir determines the home directory via libuv's uv_os_homedir. For details (for example on how to specify the home directory via environment variables), see the uv_os_homedir documentation.

source

Base.Filesystem.dirname — Function.

  1. dirname(path::AbstractString) -> AbstractString

Get the directory part of a path.

Examples

  1. julia> dirname("/home/myuser")
  2. "/home"

See also: basename

source

Base.Filesystem.basename — Function.

  1. basename(path::AbstractString) -> AbstractString

Get the file name part of a path.

Examples

  1. julia> basename("/home/myuser/example.jl")
  2. "example.jl"

See also: dirname

source

@__FILE__">Base.@FILE — Macro.

  1. @__FILE__ -> AbstractString

Expand to a string with the path to the file containing the macrocall, or an empty string if evaluated by julia -e <expr>. Return nothing if the macro was missing parser source information. Alternatively see PROGRAM_FILE.

source

@__DIR__">Base.@DIR — Macro.

  1. @__DIR__ -> AbstractString

Expand to a string with the absolute path to the directory of the file containing the macrocall. Return the current working directory if run from a REPL or if evaluated by julia -e <expr>.

source

@__LINE__">Base.@LINE — Macro.

  1. @__LINE__ -> Int

Expand to the line number of the location of the macrocall. Return 0 if the line number could not be determined.

source

Base.Filesystem.isabspath — Function.

  1. isabspath(path::AbstractString) -> Bool

Determine whether a path is absolute (begins at the root directory).

Examples

  1. julia> isabspath("/home")
  2. true
  3. julia> isabspath("home")
  4. false

source

Base.Filesystem.isdirpath — Function.

  1. isdirpath(path::AbstractString) -> Bool

Determine whether a path refers to a directory (for example, ends with a path separator).

Examples

  1. julia> isdirpath("/home")
  2. false
  3. julia> isdirpath("/home/")
  4. true

source

Base.Filesystem.joinpath — Function.

  1. joinpath(parts...) -> AbstractString

Join path components into a full path. If some argument is an absolute path or (on Windows) has a drive specification that doesn't match the drive computed for the join of the preceding paths, then prior components are dropped.

Examples

  1. julia> joinpath("/home/myuser", "example.jl")
  2. "/home/myuser/example.jl"

source

Base.Filesystem.abspath — Function.

  1. abspath(path::AbstractString) -> AbstractString

Convert a path to an absolute path by adding the current directory if necessary. Also normalizes the path as in normpath.

source

  1. abspath(path::AbstractString, paths::AbstractString...) -> AbstractString

Convert a set of paths to an absolute path by joining them together and adding the current directory if necessary. Equivalent to abspath(joinpath(path, paths…)).

source

Base.Filesystem.normpath — Function.

  1. normpath(path::AbstractString) -> AbstractString

Normalize a path, removing "." and ".." entries.

Examples

  1. julia> normpath("/home/myuser/../example.jl")
  2. "/home/example.jl"

source

Base.Filesystem.realpath — Function.

  1. realpath(path::AbstractString) -> AbstractString

Canonicalize a path by expanding symbolic links and removing "." and ".." entries.

source

Base.Filesystem.relpath — Function.

  1. relpath(path::AbstractString, startpath::AbstractString = ".") -> AbstractString

Return a relative filepath to path either from the current directory or from an optional start directory. This is a path computation: the filesystem is not accessed to confirm the existence or nature of path or startpath.

source

Base.Filesystem.expanduser — Function.

  1. expanduser(path::AbstractString) -> AbstractString

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

source

Base.Filesystem.splitdir — Function.

  1. splitdir(path::AbstractString) -> (AbstractString, AbstractString)

Split a path into a tuple of the directory name and file name.

Examples

  1. julia> splitdir("/home/myuser")
  2. ("/home", "myuser")

source

Base.Filesystem.splitdrive — Function.

  1. splitdrive(path::AbstractString) -> (AbstractString, AbstractString)

On Windows, split a path into the drive letter part and the path part. On Unix systems, the first component is always the empty string.

source

Base.Filesystem.splitext — Function.

  1. splitext(path::AbstractString) -> (AbstractString, AbstractString)

If the last component of a path contains a dot, split the path into everything before the dot and everything including and after the dot. Otherwise, return a tuple of the argument unmodified and the empty string.

Examples

  1. julia> splitext("/home/myuser/example.jl")
  2. ("/home/myuser/example", ".jl")
  3. julia> splitext("/home/myuser/example")
  4. ("/home/myuser/example", "")

source

原文: https://juliacn.github.io/JuliaZH.jl/latest/base/file/