How do I get a listing of only files using Dir.glob?

前端 未结 8 544
走了就别回头了
走了就别回头了 2021-01-03 21:10

How can I return a list of only the files, not directories, in a specified directory?

I have my_list = Dir.glob(script_path.join(\"*\"))

This re

相关标签:
8条回答
  • 2021-01-03 21:35

    you can basically just get filenames with File.basename(file)

    Dir.glob(script_path.join("*")).map{ |s| File.basename(s) }
    
    0 讨论(0)
  • 2021-01-03 21:39

    You can use Dir[]/Dir.glob or Dir.entries to get file listing. The difference between them is the former returns complete path, and the latter returns only filename.

    So be careful about the following mapping segment .select {|f| File.file?(f)}: with complete path it works well, while with only filename, it sometimes works wired.

    FYR:

    Dir[], Dir.glob, Dir.entries

    0 讨论(0)
提交回复
热议问题