如何在Rails Rake任务中使用File.open?

时间:2011-10-11 21:18:57

标签: ruby-on-rails rake

我需要创建一个定期任务,从外部文本文件创建(或编辑)产品记录。来自irb:

>> f = File.open(<filename>) # file in same directory path

没问题。

但是当粘贴到Rake任务文件时,脚本总是炸弹“未找到文件”。 (Rails 3.1,Ubuntu。)

namespace :sap do
  desc "uploads data from raw SAP file"
  task :upload => :environment do
     f = File.open("sap_pnlist_20111010a.csv")
     records = f.readlines
     records.each {|row|
     ... etc etc ...
     }
  end
end

建议?

1 个答案:

答案 0 :(得分:5)

如果文件位于Rails根目录中的某个位置,请使用

IDictionary<number, frequency>

如果该文件不在您的Rails根目录中,则必须为其提供完整路径