2026a
# 文件系统
文件系统,对应Base.FileSystem标准库,详见 Julia 中文文档 的Base > 文件系统章节。
# 基础函数
| 功能 | 函数 |
|---|---|
| 当前工作目录 | pwd() |
| 更改工作目录 | cd(dir::AbstractString=homedir()) |
| 读取文件夹 | readdir(dir::AbstractString=pwd(); join::Bool = false, sort::Bool = true) |
| 创建文件夹(要求中间目录存在) | mkdir(path::AbstractString; mode::Unsigned = 0o777) |
| 创建目录 | mkpath(path::AbstractString; mode::Unsigned = 0o777) |
| 查看文件状态 | stat(file) |
| 查看文件创建时间 | ctime(file) |
| 查看文件最近修改时间 | mtime(file) |
| 查看文件权限 | filemode(file) |
| 查看文件大小 | filesize(file) |
| 拷贝文件 | cp(src::AbstractString, dst::AbstractString; force::Bool=false, follow_symlinks::Bool=false) |
| 移动文件 | mv(src::AbstractString, dst::AbstractString; force::Bool=false) |
| 删除文件 | rm(path::AbstractString; force::Bool=false, recursive::Bool=false) |
| 创建文件 | touch(path::AbstractString) |
| 临时目录 | tempdir() |
| home目录 | homedir() |
| 判断是否为文件夹 | isdir(path) |
| 判断是否为文件 | isfile(path) |
| 判断是否为路径 | ispath(path) |
| 获取文件夹名称 | dirname(path::AbstractString) |
| 获取文件名称 | basename(path::AbstractString) |
| 判断是否绝对路径 | isabspath(path::AbstractString) |
| 路径连接 | joinpath(parts::AbstractString...)joinpath(parts::Vector{AbstractString})joinpath(parts::Tuple{AbstractString}) |
| 规范路径 | normpath(path::AbstractString) |
| 获取相对路径 | relpath(path::AbstractString, startpath::AbstractString = ".") |
| 切分路径 | splitpath(path::AbstractString) |
| 切分文件扩展名 | splitext(path::AbstractString) |
| 路径分隔符 | Base.Filesystem.path_separator |
# 示例 - 文件夹操作
本示例主要展示如何创建一个嵌套子文件夹、子文件的目录,并对该目录进行遍历,最后以列表形式进行打印显示(类似powershell的ls命令)。通过该示例,可以了解一些文件或文件夹的常规操作,包括创建目录、创建文件、拷贝文件、查看文件属性、计算相对路径等。
using Dates
using TimeZones
# 创建目录
my_dir = joinpath(tempdir(), "my_dir")
!isdir(my_dir) && mkpath(my_dir)
# 创建子文件
sub_file = joinpath(my_dir, "sub_file.txt")
!isfile(sub_file) && touch(sub_file)
open(sub_file,"w") do io
write(io, "Hello, TongYuan!")
end
# 创建子目录
sub_dir = joinpath(my_dir, "sub_dir")
!isdir(sub_dir) && mkdir(sub_dir)
# 拷贝文件
cp(sub_file, joinpath(sub_dir, "sub_sub_file.txt"), force=true)
cp(sub_file, joinpath(sub_dir, "sub_sub_file2.txt"), force=true)
# unix时间戳格式化
function format_time(x)
zdt = ZonedDateTime(unix2datetime(x), localzone(); from_utc=true)
return Dates.format(zdt, "yyyy/mm/dd HH:MM")
end
# 遍历目录
function traverse_dir(path::AbstractString, start_path::AbstractString; recursive=true, level=0)
# 文件属性
st = stat(path)
str_mode = Base.Filesystem.filemode_string(st.mode)
str_mtime = format_time(st.mtime)
str_size = isfile(path) ? string(st.size) : "" # 不统计文件夹大小
for i in [str_mode, str_mtime, str_size]
print(i)
residue = 20 - length(i)
if residue > 0
print(repeat(" ", residue))
end
print(" ")
end
# 相对路径
str_rel = relpath(path, start_path)
println(str_rel)
# 递归遍历子目录
if recursive && isdir(path) && !startswith(basename(path), ".") # 忽略以"."开头的文件夹
sub_list = readdir(path; join=true)
for sub in sub_list
traverse_dir(sub, start_path; recursive=recursive, level=level + 1)
end
end
end
function traverse_dir(path::AbstractString)
println("Mode LastWriteTime Length Name")
println("---- ------------- ------ ----")
traverse_dir(path, path)
end
traverse_dir(my_dir)
#= 打印结果:
Mode LastWriteTime Length Name
---- ------------- ------ ----
drw-rw-rw- 2024/06/19 17:14 .
drw-rw-rw- 2024/06/19 18:38 sub_dir
-rw-rw-rw- 2024/06/19 18:38 16 sub_dir\sub_sub_file.txt
-rw-rw-rw- 2024/06/19 18:38 16 sub_dir\sub_sub_file2.txt
-rw-rw-rw- 2024/06/19 18:38 16 sub_file.txt
=#