I had this issue when trying to create a file in Ruby.
I was trying to use the command below to create a new file:
File.new("testfile", "r")
and
File.open("testfile", "r")
but then I was getting the error below:
(irb):1:in `initialize': No such file or directory @ rb_sysopen - testfile (Errno::ENOENT)
Here's how I fixed it:
The issue was that I did not specify the right mode for the file. The format for creating new files is:
File.new(filename, mode)
OR
File.open(filename, mode)
And the various modes are:
"r" Read-only, starts at beginning of file (default mode).
"r+" Read-write, starts at beginning of file.
"w" Write-only, truncates existing file
to zero length or creates a new file for writing.
"w+" Read-write, truncates existing file to zero length
or creates a new file for reading and writing.
"a" Write-only, each write call appends data at end of file.
Creates a new file for writing if file does not exist.
"a+" Read-write, each write call appends data at end of file.
Creates a new file for reading and writing if file does
not exist.
However, my command File.new("testfile", "r")
was using the "r" Read-only
mode which was trying to read from an existing file called testfile
instead of creating a new one. All I had to do was to modify the command to use the "w" Write-only
mode:
File.new("testfile", "w")
OR
File.open("testfile", "w")
Reference: File in Ruby