朱莉娅 "No such file or directory"

Julia "No such file or directory"

我在一个文件夹中有一些 .txt 文件,该文件夹不是我的脚本所在的文件夹。但是当我尝试打开这些文件时,我得到 LoadError: SystemError: opening file "/some/folder/filename.txt": No such file or directory

path = "/some/folder/"
files = filter(file -> endswith(file, ".txt"), readdir(path))

for file in files
    open(file, "r")
end

如果我只是在 for 循环中执行 println(file),我可以看到文件在那里。但是,如果我尝试对文件执行任何操作,则会出现此错误。我已经使用 pwd() 来获取正确的目录。当我收到此错误时真的很困惑。

来自文档字符串:

help?> readdir
search: readdir

  readdir(dir::AbstractString=pwd();
      join::Bool = false,
      sort::Bool = true,
  ) -> Vector{String}

  Return the names in the directory dir or the current working directory if not given. When join is false, readdir returns just the names in the directory as is; when join is true, it returns joinpath(dir,
  name) for each name so that the returned strings are full paths. If you want to get absolute paths back, call readdir with an absolute directory path and join set to true.

即您希望 readdir(path; join = true) 获取文件的完整路径。