【问题标题】:No such file or directory @ rb_sysopen没有这样的文件或目录@rb_sysopen
【发布时间】:2014-05-14 03:48:44
【问题描述】:

我正在使用 Ruby 2.1.1 运行此代码时:

<CSV.foreach("public/data/original/example_data.csv",headers: true, converters:              :numeric) do |info|

我收到一个错误:

No such file or directory @ rb_sysopen

如果我将example_data.csv 放在同一个目录中,如下图所示,但我的老板说不可能这样,他希望所有*.csv 文件都放在不同的目录中:

<CSV.foreach("example_data.csv",headers: true, converters: :numeric) do |info|

【问题讨论】:

  • 您需要使用一种叫做“相对路径”的东西。
  • 您能否再详细说明一下,结果证明这是一场噩梦,Rails 4、Ruby 2.1.1 FileUtils 似乎在我尝试解决方法时坏了...#FileUtils.move '/public/ data/original', '/controllers' #require File.expand_path('../app/public/data/original/', FILE)

标签: ruby csv


【解决方案1】:

我不得不使用绕过文件实用程序的解决方法。使用 thinkbot/paperclip 生成了一个名为 csvcontroller 的目录。我将 csv 文件放在该目录文件夹中。

class Uploader < ActiveRecord::Base
attr_accessible :purchase_name, :item_description, :item_price, :purchase_count,
                  :merchant_address, :merchant_name, :csvdata

has_attached_file :csvdata, :url => "/csvcontroller/:basename.:extension",

                :path => ":rails_root/csvcontroller/:basename.:extension"

                #:default_url => "/controllers/original/example_data.csv"

  validates_attachment_content_type :csvdata, :content_type => ["text/csv"]

end

然后我将解析器放在该目录中以避免使用 FileUtils

require 'csv'

@total_cost = 0

#errors out FileUtils.move '/public/data/original/example_data.csv', '/controllers'

#errors out require File.expand_path('../app/public/data/original/', __FILE__)

# errors outCSV.foreach("any_path_name_outside_the_same_directory/example_data.csv", 
  #headers: true, converters: :numeric) do    |info|

CSV.foreach("example_data.csv", headers: true, converters: :numeric) do |info|

a =(info["item price"]).to_f

b = (info["purchase count"]).to_i

@total_cost += a * b
@store = []
customer = []
customer << info["purchaser name"]
@store << info["item description"]
@store << (info["item price"]).to_f
@store << (info["purchase count"]).to_i
@store << info["merchant address"]
@store << info["merchant name"]
puts @customer
puts @store
puts @total_cost
end

它看起来很丑,但就是这样。 我无法让 FileUtils:: 类正常工作。这是 2.1.1 的 Ruby 错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2014-07-09
    • 2018-05-01
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多