搜索文件夹中的文件

MDe*_*MDe 22 julia

我正在尝试使用Julia解析大量文本文件,并且我想循环遍历文件名数组,而不是键入函数调用来单独读取每个文件.到目前为止,我一直无法找到一种方法来搜索文件夹中与模式匹配的文件.

是否有一个基础库Julia函数,它将返回给定文件夹中的所有文件名,匹配给定的字符串模式?

R中的等效函数是list.files(),如果这有助于传达我想要的东西.

Isa*_*ton 41

在朱莉娅,相当于list.files()readdir([path])

我知道没有内置的目录搜索,但它是一个单行:

searchdir(path,key) = filter(x->contains(x,key), readdir(path))

  • 作为补充:类似于`contains`,以下功能可能有用:`isfile`,`isdir`,`startswith`,`endswith`。 (2认同)

Tro*_*ock 5

另一种解决方案是使用Glob.jl包。例如,如果您的目录中包含以下文件列表:

foo1.txt
foo2.txt
foo3.txt
bar1.txt
foo.jl
Run Code Online (Sandbox Code Playgroud)

并且您想查找所有以“ foo”开头的文本文件

using Glob
glob("foo*.txt") #if searching the working directory
#output:
#"foo1.txt"
#"foo2.txt"
#"foo3.txt"
glob("foo*.txt","path/to/dir") #for specifying a different directory
#output:
#"path/to/dir/foo1.txt"
#"path/to/dir/foo2.txt"
#"path/to/dir/foo3.txt"
Run Code Online (Sandbox Code Playgroud)