The file system
#
Base.read
— Method
read(filename::AbstractString)
Reads the entire contents of the file as Vector{UInt8}
.
read(filename::AbstractString, String)
Reads the entire contents of the file as a string.
read(filename::AbstractString, args...)
Opens the file and reads its contents. 'args` is passed to read
: this is equivalent to open(io->read(io, args...), filename)
.
#
Base.write
— Method
write(filename::AbstractString, content)
Writes the canonical binary representation of `content' to a file that will be created if it does not already exist, or overwritten otherwise.
Returns the number of bytes written to the file.
#
Base.Filesystem.cd
— Method
cd(f::Function, dir::AbstractString=homedir())
Temporarily changes the current working directory to dir
, applies the f
function, and returns to the source directory.
Examples
julia> pwd()
"/home/JuliaUser"
julia> cd(readdir, "/home/JuliaUser/Projects/julia")
34-element Array{String,1}:
".circleci"
".freebsdci.sh"
".git"
".gitattributes"
".github"
⋮
"test"
"ui"
"usr"
"usr-staging"
julia> pwd()
"/home/JuliaUser"
#
Base.Filesystem.readdir
— Function
readdir(dir::AbstractString=pwd();
join::Bool = false,
sort::Bool = true,
) -> Vector{String}
Returns names in the dir
directory or the current working directory if it is not specified. If join
is set to false, readdir
returns only the names in the directory in their original form; if join
is set to true, it returns joinpath(dir, name)
for each name', so the returned strings are full paths. To get the absolute paths again, call `readdir
with the absolute directory path and `join' set to true.
By default, readdir' sorts the list of returned names. To skip sorting names and get them in the order they are listed in the file system, you can use `readdir(dir, sort=false)
.
See also the description walkdir
.
Compatibility: Julia 1.4
Named arguments |
Examples
julia> cd("/home/JuliaUser/dev/julia")
julia> readdir()
30-element Array{String,1}:
".appveyor.yml"
".git"
".gitattributes"
⋮
"ui"
"usr"
"usr-staging"
julia> readdir(join=true)
30-element Array{String,1}:
"/home/JuliaUser/dev/julia/.appveyor.yml"
"/home/JuliaUser/dev/julia/.git"
"/home/JuliaUser/dev/julia/.gitattributes"
⋮
"/home/JuliaUser/dev/julia/ui"
"/home/JuliaUser/dev/julia/usr"
"/home/JuliaUser/dev/julia/usr-staging"
julia> readdir("base")
145-element Array{String,1}:
".gitignore"
"Base.jl"
"Enums.jl"
⋮
"version_git.sh"
"views.jl"
"weakkeydict.jl"
julia> readdir("base", join=true)
145-element Array{String,1}:
"base/.gitignore"
"base/Base.jl"
"base/Enums.jl"
⋮
"base/version_git.sh"
"base/views.jl"
"base/weakkeydict.jl"
julia> readdir(abspath("base"), join=true)
145-element Array{String,1}:
"/home/JuliaUser/dev/julia/base/.gitignore"
"/home/JuliaUser/dev/julia/base/Base.jl"
"/home/JuliaUser/dev/julia/base/Enums.jl"
⋮
"/home/JuliaUser/dev/julia/base/version_git.sh"
"/home/JuliaUser/dev/julia/base/views.jl"
"/home/JuliaUser/dev/julia/base/weakkeydict.jl"
#
Base.Filesystem.walkdir
— Function
walkdir(dir; topdown=true, follow_symlinks=false, onerror=throw)
Returns an iterator that traverses the directory tree. The iterator returns a tuple containing (rootpath, dirs, files)
. The directory tree can be traversed from top to bottom or from bottom to top. If walkdir
or stat
detects an IOError', the default error will be re-output. The user error handling function can be provided using the named 'onerror
argument. 'onerror` is called with IOError
as an argument.
See also the description readdir
.
Examples
for (root, dirs, files) in walkdir(".")
println("Directories in $root")
for dir in dirs
println(joinpath(root, dir)) # путь к каталогам
end
println("Files in $root")
for file in files
println(joinpath(root, file)) # путь к файлам
end
end
julia> mkpath("my/test/dir");
julia> itr = walkdir("my");
julia> (root, dirs, files) = first(itr)
("my", ["test"], String[])
julia> (root, dirs, files) = first(itr)
("my/test", ["dir"], String[])
julia> (root, dirs, files) = first(itr)
("my/test/dir", String[], String[])
#
Base.Filesystem.mkdir
— Function
mkdir(path::AbstractString; mode::Unsigned = 0o777)
Creates a directory named path
with mode
permissions. The default value of mode' is `0o777'. It is changed 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 returns an error. For information about the function that creates all the necessary intermediate directories, see the description `mkpath
. Returns the `path'.
Examples
julia> mkdir("testingdir")
"testingdir"
julia> cd("testingdir")
julia> pwd()
"/home/JuliaUser/testingdir"
#
Base.Filesystem.mkpath
— Function
mkpath(path::AbstractString; mode::Unsigned = 0o777)
Creates all necessary intermediate directories in the path
. Directories are created with the mode
permissions, which have the default value of 0o777
and are modifiable by the current file creation mask. As opposed to mkdir
, mkpath' does not return an error if the `path' (or parts of it) already exists. However, an error is thrown if the `path
(or parts of it) points to an existing file. Returns the `path'.
If path
contains a file name, you probably want to use `mkpath(dirname(path))' to avoid creating a directory using the file name.
Examples
julia> cd(mktempdir())
julia> mkpath("my/test/dir") # создает три каталога
"my/test/dir"
julia> readdir()
1-element Array{String,1}:
"my"
julia> cd("my")
julia> readdir()
1-element Array{String,1}:
"test"
julia> readdir("test")
1-element Array{String,1}:
"dir"
julia> mkpath("intermediate_dir/actually_a_directory.txt") # creates two directories
"intermediate_dir/actually_a_directory.txt "
julia> isdir("intermediate_dir/actually_a_directory.txt")
true
#
Base.Filesystem.symlink
— Function
symlink(target::AbstractString, link::AbstractString; dir_target = false)
Creates a symbolic link to a target
named `link'.
In Windows, symbolic links must be explicitly declared as pointing to a directory or not. If the target
already exists, the link
type will be determined automatically by default. However, if target
does not exist, this function will create a symbolic link to the file by default, unless dir_target
is set to true'. Note that if the user sets `dir_target
, but the target
exists and is a file, a symbolic link to the directory will still be created, but its dereference will fail, just as if the user had created a symbolic link to the file (by calling symlink()`with `dir_target
with the value false
before creating the directory) and tried to dereference it for the directory.
In addition, there are two ways to create a link in Windows: symbolic links and connection points. Junction points are slightly more efficient, but do not support relative paths, so if a relative symbolic link to a directory is requested (as indicated by using isabspath(target)
, which returns false
), a symbolic link will be used; otherwise, a junction point will be used. It is recommended to create symbolic links in Windows only after creating the files or directories they point to.
See also the description hardlink
.
This feature causes an error on operating systems that do not support soft symbolic links, such as Windows XP. |
Compatibility: Julia 1.6
The named argument `dir_target' was added in Julia 1.6. Before that, symbolic links to non-existent paths in Windows were always file symbolic links, and relative symbolic links to directories were not supported. |
#
Base.Filesystem.readlink
— Function
readlink(path::AbstractString) -> String
Returns the target location that the symbolic link path
points to.
#
Base.Filesystem.chmod
— Function
chmod(path::AbstractString, mode::Integer; recursive::Bool=false)
Changes the permission mode path
to mode
. Currently, only integer modes
are supported (for example, 0o777
). . If recursive=true
and the path is a directory, all permissions in that directory will be changed recursively. Returns the `path'.
Before Julia 1.6, it was impossible to work correctly with file system ACLs in Windows, so read-only bits were set in files. It is now possible to manage ACL lists. |
#
Base.Filesystem.chown
— Function
chown(path::AbstractString, owner::Integer, group::Integer=-1)
Changes the owner and/or group path
to owner
and/or group'. If the value `-1
is entered for owner
or group
, the corresponding identifier will not change. Currently, only integer owner
and group
are supported. Returns the `path'.
#
Base.stat
— Function
stat(file)
Returns a structure whose fields contain information about the file. The structure fields are listed below.
Name | Type | Description |
---|---|---|
desc |
|
OS file path or descriptor |
size |
|
File size (in bytes) |
device |
|
ID of the device containing the file |
inode |
|
The number of the file’s index descriptor |
mode |
|
File protection mode |
nlink |
|
Number of hard links per file |
uid |
|
The ID of the user — the owner of the file |
gid |
|
ID of the file owner’s group |
rdev |
|
If this file links to a device, the ID of the device it links to is |
blksize |
|
Preferred file system block size for a file |
blocks |
|
The number of allocated 512-byte blocks |
mtime |
|
A Unix timestamp indicating the time when a file was last modified |
ctime |
|
A Unix timestamp indicating the time when file metadata was changed |
#
Base.Filesystem.diskstat
— Function
diskstat(path=pwd())
Returns statistics in bytes about the disk containing the file or directory pointed to by path
. If no arguments are passed, statistics about the disk containing the current working directory are returned.
Compatibility: Julia 1.8
This method was added in Julia 1.8. |
#
Base.Filesystem.cp
— Function
cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false)
Copies a file, link, or directory from src
to dst'. With `force=true
, the existing dst
will be deleted first.
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 pointed to by src
. Returns `dst'.
The |
#
Base.download
— Function
download(url::AbstractString, [path::AbstractString = tempname()]) -> path
Downloads the file from the specified URL and saves it in the path
location or, if not specified, in a temporary path. Returns the path of the downloaded file.
Starting with Julia 1.6, this feature is considered obsolete and is only a thin shell for |
#
Base.Filesystem.mv
— Function
mv(src::AbstractString, dst::AbstractString; force::Bool=false)
Moves a file, link, or directory from src
to dst'. With `force=true
, the existing dst
will be deleted first. Returns `dst'.
Examples
julia> write("hello.txt", "world");
julia> mv("hello.txt", "goodbye.txt")
"goodbye.txt"
julia> "hello.txt" in readdir()
false
julia> readline("goodbye.txt")
"world"
julia> write("hello.txt", "world2");
julia> mv("hello.txt", "goodbye.txt")
ERROR: ArgumentError: 'goodbye.txt' exists. `force=true` is required to remove 'goodbye.txt' before moving.
Stacktrace:
[1] #checkfor_mv_cp_cptree#10(::Bool, ::Function, ::String, ::String, ::String) at ./file.jl:293
[...]
julia> mv("hello.txt", "goodbye.txt", force=true)
"goodbye.txt"
julia> rm("goodbye.txt");
#
Base.Filesystem.rm
— Function
rm(path::AbstractString; force::Bool=false, recursive::Bool=false)
Deletes a file, link, or empty directory using the specified path. If force=true
is passed, a non-existent path is not considered an error. If recursive=true
is passed and the path is a directory, all contents are recursively deleted.
Examples
julia> mkpath("my/test/dir");
julia> rm("my", recursive=true)
julia> rm("this_file_does_not_exist", force=true)
julia> rm("this_file_does_not_exist")
ERROR: IOError: unlink("this_file_does_not_exist"): no such file or directory (ENOENT)
Stacktrace:
[...]
#
Base.Filesystem.touch
— Function
Base.touch(::Pidfile.LockMonitor)
Updates the `mtime' for the lock to indicate that it is still up to date.
See also the description of the refresh
keyword in the [mkpidlock
] constructor.
touch(path::AbstractString) touch(fd::File)
Updates the timestamp of the last modification in the file according to the current time.
If the file does not exist, a new file is created.
Returns the `path'.
Examples
julia> write("my_little_file", 2);
julia> mtime("my_little_file")
1.5273815391135583e9
julia> touch("my_little_file");
julia> mtime("my_little_file")
1.527381559163435e9
We see that mtime
changed by the touch
function.
#
Base.Filesystem.tempname
— Function
tempname(parent=tempdir(); cleanup=true) -> String
Creates a path to a temporary file. This function returns only the path, the file is not created. The path is likely to be unique, but this is not guaranteed due to the very low probability that two simultaneous calls to tempname
will create the same file name. The name is guaranteed to differ from the names of all files that already exist at the time of calling tempname
.
When called without arguments, the temporary name will be the absolute path to the temporary name in the system temporary directory specified by tempdir()'. If the `parent
directory argument is set, the temporary path will be located in this directory.
The cleanup
parameter controls whether the process tries to automatically delete the returned path at completion. Note that the tempname
function does not create any file or directory in the returned location, so cleanup is performed only after the file or directory is created there. If you do this and the cleanup
parameter is set to `true', the path will be deleted at the end of the process.
Compatibility: Julia 1.4
The arguments |
There may be security breaches if another process gets the same file name and creates the file before you do. If this situation is a concern, open the file using |
#
Base.Filesystem.tempdir
— Function
tempdir()
Returns the path to the temporary directory. In Windows` 'tempdir()` uses the first environment variable found in the ordered list TMP
, TEMP
, USERPROFILE'. In all other operating systems, tempdir() uses the first environment variable found in the ordered list of TMPDIR, TMP, TEMP, and TEMPDIR. If nothing is found, the path `"/tmp"
is used.
#
Base.Filesystem.mktemp
— Method
mktemp(parent=tempdir(); cleanup=true) -> (path, io)
Returns (path, io)
, where path
is the path to the new temporary file in parent
, and io
is the object of the open file for this path. The 'cleanup` parameter controls the automatic deletion of a temporary file at the end of the process.
Compatibility: Julia 1.3
The named |
#
Base.Filesystem.mktemp
— Method
mktemp(f::Function, parent=tempdir())
Applies the function f
to the result mktemp(parent)
and deletes the temporary file upon completion.
See also the description mktempdir
.
#
Base.Filesystem.mktempdir
— Method
mktempdir(parent=tempdir(); prefix="jl_", cleanup=true) -> path
Creates a temporary directory in the parent
directory with a name based on the specified prefix prefix
and a random suffix, and returns the path to it. Also, on some platforms, all end characters are They can be replaced with random characters in the prefix
. If the parent
does not exist, an error occurs. The 'cleanup` parameter controls the automatic deletion of the temporary directory at the end of the process.
Compatibility: Julia 1.2
The named |
Compatibility: Julia 1.3
The named |
#
Base.Filesystem.mktempdir
— Method
mktempdir(f::Function, parent=tempdir(); prefix="jl_")
Applies the function f
to the result mktempdir(parent; prefix)
and deletes the temporary directory and all its contents upon completion.
Compatibility: Julia 1.2
The named |
#
Base.Filesystem.isblockdev
— Function
isblockdev(path) -> Bool
Returns true
if the path' is a block device, otherwise it returns `false
.
#
Base.Filesystem.ischardev
— Function
ischardev(path) -> Bool
Returns true
if path' is a character device, otherwise it returns `false
.
#
Base.Filesystem.isfifo
— Function
isfifo(path) -> Bool
Returns true
if the path
is FIFO, otherwise it returns false
.
#
Base.Filesystem.isfile
— Function
isfile(path) -> Bool
Returns true
if the path' is a regular file, otherwise it returns `false
.
Examples
julia> isfile(homedir())
false
julia> filename = "test_file.txt";
julia> write(filename, "Hello world!");
julia> isfile(filename)
true
julia> rm(filename);
julia> isfile(filename)
false
#
Base.Filesystem.islink
— Function
islink(path) -> Bool
Returns true
if path
is a symbolic link, otherwise it returns false
.
#
Base.Filesystem.ismount
— Function
ismount(path) -> Bool
Returns true
if path
is the connection point, otherwise it returns `false'.
#
Base.Filesystem.issetgid
— Function
issetgid(path) -> Bool
Returns true
if the setgid flag is set for path
, otherwise it returns false
.
#
Base.Filesystem.issetuid
— Function
issetuid(path) -> Bool
Returns true
if the setuid flag is set for path
, otherwise it returns false
.
#
Base.Filesystem.issocket
— Function
issocket(path) -> Bool
Returns true
if path
is a socket, otherwise it returns `false'.
#
Base.Filesystem.issticky
— Function
issticky(path) -> Bool
Returns true
if the pinning bit is set for path
, otherwise it returns false
.
#
Base.Filesystem.homedir
— Function
homedir() -> String
Returns the current user’s home directory.
'homedir` defines the home directory using |
See also the description Sys.username
.
#
Base.Filesystem.dirname
— Function
dirname(path::AbstractString) -> String
Returns the part of the path containing the folder. The end characters ('/' or ") in the path are considered part of the path.
Examples
julia> dirname("/home/myuser")
"/home"
julia> dirname("/home/myuser/")
"/home/myuser"
See also the description basename
.
#
Base.Filesystem.basename
— Function
basename(path::AbstractString) -> String
Returns the part of the path containing the file name.
This function is slightly different from the Unix program |
Examples
julia> basename("/home/myuser/example.jl")
"example.jl"
julia> basename("/home/myuser/")
""
See also the description dirname
.
#
Base.Filesystem.isabspath
— Function
isabspath(path::AbstractString) -> Bool
Determines whether the path is absolute (starts from the root directory).
Examples
julia> isabspath("/home")
true
julia> isabspath("home")
false
#
Base.Filesystem.isdirpath
— Function
isdirpath(path::AbstractString) -> Bool
Determines whether the path belongs to a directory (for example, it ends with a path separator).
Examples
julia> isdirpath("/home")
false
julia> isdirpath("/home/")
true
#
Base.Filesystem.joinpath
— Function
joinpath(parts::AbstractString...) -> String
joinpath(parts::Vector{AbstractString}) -> String
joinpath(parts::Tuple{AbstractString}) -> String
Combines path components into a complete path. If any argument is an absolute path or (in Windows) has a disk specification that does not match the disk defined for combining previous paths, the previous components are discarded.
Note that since there is a current directory for each disk in Windows, joinpath("c:", "foo")
represents the path relative to the current directory on the c: disk, so it looks like c:foo, not c:\foo . Moreover, joinpath
treats it as a non-absolute path and ignores the case of the drive letter, so joinpath("C:\A","c:b") = "C:\A\b"
.
Examples
julia> joinpath("/home/myuser", "example.jl")
"/home/myuser/example.jl"
julia> joinpath(["/home/myuser", "example.jl"])
"/home/myuser/example.jl"
#
Base.Filesystem.abspath
— Function
abspath(path::AbstractString) -> String
Converts the path to an absolute path with the addition of the current directory, if necessary. It also normalizes the path as in normpath
.
Examples
If you are in the JuliaExample
directory, and the data you are using is two levels higher relative to the JuliaExample
directory, you can write:
abspath("../../data")
The result will be a path like `"/home/JuliaUser/data/"'.
See also the description joinpath
, pwd
and expanduser
.
abspath(path::AbstractString, paths::AbstractString...) -> String
Converts a set of paths to an absolute path, combining them and adding the current directory, if necessary. Equivalent to abspath(joinpath(path, paths...))
.
#
Base.Filesystem.normpath
— Function
normpath(path::AbstractString) -> String
Normalizes the path by removing the "." and ".." elements and changing the "/" to the canonical path separator for the system.
Examples
julia> normpath("/home/myuser/../example.jl")
"/home/example.jl"
julia> normpath("Documents/Julia") == joinpath("Documents", "Julia")
true
normpath(path::AbstractString, paths::AbstractString...) -> String
Converts a set of paths into a normalized path with their union and the removal of the elements "." and "..". Equivalent to normpath(joinpath(path, paths...))
.
#
Base.Filesystem.realpath
— Function
realpath(path::AbstractString) -> String
Canonicalizes the path by extending symbolic links and removing the "." and ".." elements. In case-sensitive and case-insensitive file systems (usually Mac and Windows), the case stored in the file system is returned for the path.
(This function throws an exception if the `path' does not exist in the file system.)
#
Base.Filesystem.relpath
— Function
relpath(path::AbstractString, startpath::AbstractString = ".") -> String
Returns the relative file path for path
either from the current directory or from an optional start directory. The path is calculated: there are no calls to the file system to confirm the existence or nature of path
or `startpath'.
In Windows, case is taken into account for all parts of the path, except for the drive letters. If path
and startpath
refer to different disks, the absolute path path
is returned.
#
Base.Filesystem.expanduser
— Function
expanduser(path::AbstractString) -> AbstractString
On Unix systems, it replaces the tilde character at the beginning of the path with the current user’s home directory.
See also the description contractuser
.
#
Base.Filesystem.contractuser
— Function
contractuser(path::AbstractString) -> AbstractString
If the path starts with homedir()
on Unix systems, replace it with a tilde character.
See also the description expanduser
.
#
Base.Filesystem.samefile
— Function
samefile(path_a::AbstractString, path_b::AbstractString)
Checks whether the paths path_a
and path_b
refer to the same existing file or directory.
#
Base.Filesystem.splitdir
— Function
splitdir(path::AbstractString) -> (AbstractString, AbstractString)
Splits the path into a tuple of the directory name and the file name.
Examples
julia> splitdir("/home/myuser")
("/home", "myuser")
#
Base.Filesystem.splitdrive
— Function
splitdrive(path::AbstractString) -> (AbstractString, AbstractString)
In Windows, it divides the path into a part with a drive letter and a part with a path. On Unix systems, the first component is always an empty string.
#
Base.Filesystem.splitext
— Function
splitext(path::AbstractString) -> (String, String)
If the last component of the path contains one or more points, it divides the path into everything that is before the last point, and everything starting from this point onwards. Otherwise, a tuple of an unchanged argument and an empty string is returned. splitext is short for split extension, split by extension.
Examples
julia> splitext("/home/myuser/example.jl")
("/home/myuser/example", ".jl")
julia> splitext("/home/myuser/example.tar.gz")
("/home/myuser/example.tar", ".gz")
julia> splitext("/home/my.user/example")
("/home/my.user/example", "")
#
Base.Filesystem.splitpath
— Function
splitpath(path::AbstractString) -> Vector{String}
Divides the file path into all components. This is the reverse function of `joinpath'. Returns an array of substrings, one for each directory or file in the path, including the root directory (if any).
Compatibility: Julia 1.1
This feature requires a Julia version of at least 1.1. |
Examples
julia> splitpath("/home/myuser/example.jl")
4-element Vector{String}:
"/"
"home"
"myuser"
"example.jl"