Csv.rb:1342:in `initialize': No such file or directory

ruby-1.9.3-p286

I must be making a very stupid mistake but the following piece of code
gives an error

/Users/soichi/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/csv.rb:1342:in
initialize': No such file or directory - ~/Dropbox/Ruby/FlightLuggage/airport.csv (Errno::ENOENT) from /Users/soichi/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/csv.rb:1342:inopen’
from
/Users/soichi/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/csv.rb:1342:in
open' from csv_manipulate.v1.rb:11:in

the code is

require ‘csv’

dir = “~/Dropbox/Ruby/FlightLuggage/”
file = “airport.csv”
filename = dir + file

csv_line_read = CSV.open(filename)
header = csv_line_read.shift

csv_line_read.each do |row|
p row
end

Could anyone point out the mistake I am making here? I have
double-checked the file directory.

soichi

On 29/10/2012 19:05, Soichi I. wrote:

dir = “~/Dropbox/Ruby/FlightLuggage/”
file = “airport.csv”
filename = dir + file

csv_line_read = CSV.open(filename)

The tilde (~) expansion to the home directory name is a feature provided
by the shell. The operating system knows nothing about tilde. You’ll
need something like ENV[“HOME”]+"/Dropbox/Ruby/FlightLuggage" instead.

Graham

I must be making a very stupid mistake but the following piece of code
gives an error
dir = “~/Dropbox/Ruby/FlightLuggage/”

I think you should replace ~ with ENV[‘HOME’] !

saji

On Mon, Oct 29, 2012 at 5:05 PM, Soichi I. [email protected]
wrote:

/Users/soichi/.rvm/rubies/ruby-1.9.3-p286/lib/ruby/1.9.1/csv.rb:1342:in
dir = “~/Dropbox/Ruby/FlightLuggage/”

Could anyone point out the mistake I am making here? I have
double-checked the file directory.

soichi


Posted via http://www.ruby-forum.com/.

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: [email protected]
url: http://www.u-aizu.ac.jp
bib: Web of Science

Thanks everyone. That was simple.
I got to be careful about these things…

soichi

Am 29.10.2012 09:05, schrieb Soichi I.:

dir = “~/Dropbox/Ruby/FlightLuggage/”
file = “airport.csv”
filename = dir + file

this should also work (cross platform):

filename = File.expand_path(dir + file)

or use Dir.home

dir = “~/Dropbox/Ruby/FlightLuggage/”
file = “airport.csv”
filename = dir + file

this should also work (cross platform):

filename = File.expand_path(dir + file)

or use Dir.home

Oh, thanks!

soichi

The exact same code works for me with a different path.
Must be a Tilda (~) issue then.
I agree with Graham below.