I am trying to create the logfile under log directory in my ruby script but it is returning org.jruby.exceptions.RaiseException: (Errno::ENOENT) file:/home/ABC/tool/log/file_name.log
error. Below is the piece the code where I am trying to do the same.
FileUtils.mkdir 'log'
$LOG_PATH = "#{File.expand_path("../../../log",__FILE__)}"
File.new("#{$LOG_PATH}/file_name.log")
file = File.open('file_name.log', File::APPEND)
logger = Logger.new(file, 'daily')
The above code is creating the log folder but it is not creating the file_name.log and giving Errno::ENOENT error.
Can someone guide me creating file_name.log under log folder?
Also I tried to the run the code copied below to see if it is able to create the log file under current directory and it is working fine.
$LOG = Logger.new('file_name.log', 'daily')
I tried the following and it worked.
FileUtils.mkdir 'log'
$LOG_PATH = "#{File.expand_path("../../../log",__FILE__)}"
Dir.chdir "#{$LOG_PATH}"
$LOG = Logger.new('file_name.log', 'daily')
Related
I'm trying to write an RSpec test that confirms an error message is sent to stderr when a problem occurs. My solution was to have stderr output to a file:
$stderr = File.new(File.join(#output_path, 'stderr.txt'), 'w')
Eventually when my test causes an error to occur, the stderr.txt file contains this string:
envelope_evaluation.rb: Could not initialize variables
But, when I try to extract this string from stderr.txt via this line:
contents = IO.read("#{#output_path}/stderr.txt")
The contents variable is always empty. Am I missing an IO step?
NEW EDIT
After the discussion below, I've cleaned my code up a bit, but I'm still experiencing an empty string result. When I redirect $stderr to my error file, I now am doing it in read/write mode:
$stderr = File.new("#{#output_path}/stderr.txt", 'w+')
After that, my envelope_evaluation.rb script executes and the stderr.txt file is populated with an error message. Since $stderr now has read rights, I then get its contents via:
$stderr.flush
contents = $stderr.read
Unfortunately the 'contents' variable is still empty though.
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')
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 )
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.
I am trying to create a simple RSS parser using the two frameworks. However I am getting PHPerrors when trying to write to my cache directory:
set_cache_location(APPPATH.'cache/rss');
I am running windows 7 with XAMPP using the latest version of Simplepie from github
error:
A PHP Error was encountered
Severity: User Warning
Message: C:\xampp\htdocs\geekurls/system/application/cache/rss is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.
Filename: libraries/simplepie.php
Line Number: 1732
Tried like the below comment said and tried making a test file but to no luck
$file = APPPATH."cache/rss/testFile.txt";
$handle = fopen($file, 'w') or die("fail");
fclose($handle);
A simple checks to find out what might be happening,
Try creating a file in this directory using standard php - this could help solve permission problems.
$this->load->helper('file');
$data = 'Some file data';
if ( ! write_file('./path/to/file.php', $data))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
Also how about using the default cache?
http://simplepie.org/wiki/faq/i_m_getting_cache_error_messages