# 当前目录
pwd()
# Julia获取正在运行的源程序文件所在的目录
dirname(@__FILE__) == "/Users/victor/"
# 切换目录
cd("/Users/victor/data")
# 将目录与文件连接成一个完整路径
joinpath(dirname(@__FILE__), "data", "pkg.csv") == "/Users/victor/data/pkg.csv"
求一个文件的绝对路径, 当前文件夹下有一个名为pkg.csv的文件
abspath("pkg.csv") == "/Users/victor/data/pkg.csv"
basename("/Users/victor/data/pkg.csv") == "pkg.csv"
dirname("/Users/victor/data/pkg.csv") == "/Users/victor/data"
删除..
或.
normpath("/Users/victor/data/../pkg.csv")=="/Users/victor/pkg.csv"
.
分割
splitext("/Users/victor/data/pkg.csv")
# ("/Users/victor/data/pkg", ".csv")
splitext("/Users/victor/data")
# ("/Users/victor/data", "")
/
分割
splitpath("/Users/victor/data/pkg.csv")
# "/"
# "Users"
# "victor"
# "data"
# "pkg.csv"
ispath(filename) # 是否存在
isdir(filename) # 判断是否是目录
isfile(filename) # 判断是否是文件
islink(filename) # 判断是否是链接
isabspath(filename) # 判断是否是绝对路径
cd("data")
readdir() # 返回指定目录的文件和子目录列表
# 4-element Vector{String}:
# "pkgs copy 2.csv"
# "pkgs copy 3.csv"
# "pkgs copy.csv"
# "pkgs.csv"
readdir(join=true) #返回绝对路径
# 4-element Vector{String}:
# "/Users/victor/data/pkgs copy 2.csv"
# "/Users/victor/data/pkgs copy 3.csv"
# "/Users/victor/data/pkgs copy.csv"
# "/Users/victor/data/pkgs.csv"
cd("../")
pwd()=="/Users/victor"
readdir("data")
# 4-element Vector{String}:
# "pkgs copy 2.csv"
# "pkgs copy 3.csv"
# "pkgs copy.csv"
# "pkgs.csv"
readdir(abspath("data"), join=true)
# 4-element Vector{String}:
# "/Users/victor/data/pkgs copy 2.csv"
# "/Users/victor/data/pkgs copy 3.csv"
# "/Users/victor/data/pkgs copy.csv"
# "/Users/victor/data/pkgs.csv"
walkdir(path) # 返回一个可迭代对象,包括当前目录、其中的子目录列表、其中的文件列表。
for (root, dirs, files) in walkdir("data")
println("Directories in $root")
for dir in dirs
println(joinpath(root, dir)) # path to directories
end
println("Files in $root")
for file in files
println(joinpath(root, file)) # path to files
end
end
# Directories in data
# Files in data
# data/pkgs copy 2.csv
# data/pkgs copy 3.csv
# data/pkgs copy.csv
# data/pkgs.csv
# 如果目录已经存在会报错
mkdir("newdir/newdir2")
# 如果目录已经存在不会报错
mkpath("newdir/newdir2")
touch("newfile.csv")
把pkg.csv链接到上层目录,上层目录不能存在名为pkg.csv的文件否则报错。
hardlink("/Users/victor/data/pkg.csv", "/Users/victor/pkg.csv")
symlink("/Users/victor/data/pkg.csv", "/Users/victor/pkg.csv")
如果目标已经存在,force=true会覆盖
cp("/Users/victor/data/pkg.csv", "/Users/victor/pkg.csv";force=true)
mv("/Users/victor/data/pkg.csv", "/Users/victor/pkg.csv";force=true)
# 删除文件
rm("/Users/victor/data/pkg.csv")
# 删除目录
rm("/Users/victor/data", recursive=true)
# force=true,不存在的文件不会报错
rm("/Users/victor/data/no.csv", force=true)
把文件下载到/Users/victor/data目录下
download("", "/Users/victor/data")
https://docs.julialang.org/en/v1/base/file/
联系客服