hbrovell
hbrovell

Reputation: 547

Julia "No such file or directory"

I have some .txt files in a folder that is not the folder where my script is. But when I try to open those files I get 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

If I just do a println(file) in the for loop I can see that the files are there. But if I try to do anything to the files I get this error. I have used pwd() to get the correct directory. Really confused to as I'm getting this error.

Upvotes: 1

Views: 2880

Answers (2)

Realife_Brahmin
Realife_Brahmin

Reputation: 63

In my case the Terminal as part of the editor (at the bottom of the attached screenshot) had a different directory than my currently opened .jl file. I use VSCode on Linux. I manually changed the directory in the embedded terminal as part of the VSCode editor in order for julia to detect my file. enter image description here

Upvotes: 0

Nils Gudat
Nils Gudat

Reputation: 13800

From the docstring:

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.

i.e. you want readdir(path; join = true) to get the full paths to your files.

Upvotes: 4

Related Questions