Home > Software design >  Creating a simple csv file: No such file or directory @ rb_sysopen (Errno::ENOENT)
Creating a simple csv file: No such file or directory @ rb_sysopen (Errno::ENOENT)

Time:07-13

I am struggling with a very basic task of creating a csv file from the ruby core library. I am basically copying the code almost exactly:

require 'csv'
CSV.open("~/Desktop/codes.csv", "wb") do |csv|
  csv << ["id", "code"]
  csv << ["TESTCODE01", "CODE001"]
end

And when I execute this code, I receive the following error:

csv.rb:641:in `initialize': No such file or directory @ rb_sysopen - ~/Desktop/codes.csv (Errno::ENOENT)

I have an empty csv file that I'm trying to write to on my Desktop but somehow it errors out. I know this has to be something simple that I'm not doing. Can someone point out what Im doing wrong? Thank you.

CodePudding user response:

You're expecting something like shell expansion but you're not running in a shell. Instead do something on the order of:

require 'csv'
CSV.open(Dir.home   "/Desktop/codes.csv", "wb") do |csv|
  csv << ["id", "code"]
  csv << ["TESTCODE01", "CODE001"]
end
  • Related