No such file or directory - ruby

This is the error.
Atrosity [ Eric-Raios-MacBook ][ ~/dev/rubyscripts ]$ ruby script.rb
script.rb:7:in `read': No such file or directory - sent (Errno::ENOENT)
from script.rb:7:in `lSent'
from script.rb:16:in `<main>'
My Method that is causing the error is:
def lSent
$sent = Set.new(File.read("sent").split(";"))
end
lSent
If I delete this, my script runs but does not output what I want to do.

sent should be a path to a file in your server, such as
$sent = Set.new(File.read("/root/path/file.txt").split(";"))

You're attempting to read a file called "sent", but it doesn't exist in the application's path. Try including the full path to the file.

Related

rubyzip 2.3.2 read fails on macOS 10.15.7, ruby 3.1.1p18

This fails:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
contents = zip.read(filename)
with this error:
Errno::ENOENT: No such file or directory - content.xml
This succeeds:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
while zip.size == 0
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
end
contents = zip.read(filename)
Why?
Note that #archive_io is a StringIO instance. 'filename' is an entry in the zip file known to exist of 6253 uncompressed bytes. When using Pry as a debugger, I can simply retype/rerun the open_buffer method and suddenly the zip read works. This lead to me adding the loop to re-attempt the open_buffer - a kludge of a solution.

how to use shellescape for a path with spaces in it

essentially, I have code like this (running on CentOS 6.5, ruby 2.3):
foo = "/opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml"
read_file_and_do_something_interesting(foo)
where we have:
def read_file_and_do_something_interesting(file_path)
data = File.read(file_path)
which leads to error:
/opt/provision/jobs/lib/aws_tools.rb:498:in `read': No such file or directory # rb_sysopen - /opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml (Errno::ENOENT)
So, I tried to use shellescape, like this:
read_file_and_do_something_interesting(foo.shellescape)
and still I get error:
/opt/provision/jobs/lib/aws_tools.rb:498:in `read': No such file or directory # rb_sysopen - /opt/provisioning/workspace/jobs/This\ Has\ Spaces/files/thisfile.xml (Errno::ENOENT)
So, simply, how do you use this thing?
I think this file /opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml really not exists.
Can you run ls "/opt/provisioning/workspace/jobs/This Has Spaces/files/thisfile.xml"?

Rake get directory name in file glob

If a Rake File exists with the following code:
file 'assets/*/map.a' => ['map.b', 'map.c'] do
# Code goes here...
end
I want to know what the name of the directory in the glob is (instead of the file). Any suggestions?
file 'assets/*/map.a' => ['map.b', 'map.c'] do |path|
File.basename(File.dirname(path))
end

how to read file using path in ruby by function IO.readlines("path")[0]

i want to read first line of file by using following function in ruby
IO.readlines("path")[0]
But file is not in current directory, so i use path there
puts IO.readlines("Home/Documents/vikas/SHIF.doc")
but it is giving error as
a1.rb:1:in `readlines': No such file or directory # rb_sysopen - Home/Documents/vikas/SHIF.doc (Errno::ENOENT)
from a1.rb:1:in `<main>'
You can also open a file and read only the first line instead of the entire file
File.open("Home/Documents/vikas/SHIF.doc").readline
You can use File.expand_path:
puts IO.readlines(File.expand_path("Home/Documents/vikas/SHIF.doc", __FILE__))
Note however that it will create path relatively to a file directory, not to a root directory.
If you are using rails, you could use:
puts IO.readlines(Rails.root.join 'Home', 'Documents', 'vikas', 'SHIF.doc')

Ruby - FileUtils copy_file Permission denied on Windows

I'm making gem that copy files from /template directory (inside the gem) into the current directory of the console.
Here's what it looks like:
require "fileutils"
# Get the console's current directory
destination_dir = Dir.pwd
# Home directory of my gem, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0
home_dir = File.expand_path( "..", File.dirname(__FILE__) )
# Template directory, looks like C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
template_dir = File.join( home_dir, "template" )
FileUtils.copy_file( template_dir, destination_dir )
And I got this error:
C:/Ruby193/lib/ruby/1.9.1/fileutils.rb:1370:in `initialize': Permission denied -
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template (Errno::
EACCES)
I have checked that the directory does exists by running Dir[template_dir].
Any solution? Thanks
UPDATE to answer comments below
#Babai
I added this line before copy_file, but still doesn't work. Am I doing it right?
FileUtils.chmod(0777, template_dir)
#mudasobwa
Here's the result of the code
# puts "#{template_dir} \n #{destination_dir}"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/my_gem-1.0.0/template
C:/Users/myname/Documents/Test
My bad. My template directory contains another folders. So I need to use cp_r instead of copy_file
FileUtils.cp_r( template_dir, destination_dir )

Resources