Problem in reading file ruby on rails

0

Any idea why you give this error? No such file or directory @ dir_initialize - /lib/oc/ I have several txt files in that folder my code reads any .txt file and then I use it to analyze it.

  Dir.foreach('/lib/oc/')  do |item| 
   next unless File.extname(item) == '.txt'
    next if File.directory? item
    file = File.read(item)
   File.delete(item) if File.exist?(item)
  end   
    
asked by Jonathan Zambrano 07.12.2017 в 15:05
source

1 answer

0

The main problem is the use of / at the beginning, because that indicates an absolute route and apparently you are looking for a relative route (reason why not find the directory); to fix it simply remove the% initial / :

Dir.foreach('lib/oc/') do |item| 
  # ...
end

If you're looking to run the code inside your rails application, then you could use Rails.root to get the root directory of your application and then add /lib/oc/ :

Dir.foreach("#{Rails.root}/lib/oc/") do |item| 
  # ...
end
    
answered by 07.12.2017 / 15:56
source