Prev: Speeding up TCP
Next: A regexp?
From: Robert Klemme on 4 Apr 2010 12:59 On 04.04.2010 17:30, Hawksury Gear wrote: > Hassan Schroeder wrote: >> On Sun, Apr 4, 2010 at 8:16 AM, Hawksury Gear >> <blackhawk_932(a)hotmail.com> wrote: >> >>> ... it gives an error message saying >>> >>> "Permission Denied Errno::EACCESS" >>> >>> Any idea why? >> Is there a reason you don't think it means exactly what it says? > > No doubt, the error message is self explanatory. > But the actual files themselves in the directory allow Read and Write > operations so they are not protected against any Read/Write operations.I > have asked for the read-only permission="r" which in theory it should > grant. Your error is most likely caused by the fact that Dir#entries returns file names only, i.e. without a path. You are trying to open different files than you think (most likely in the current directory). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Hawksury Gear on 4 Apr 2010 12:25 > Your error is most likely caused by the fact that Dir#entries returns > file names only, i.e. without a path. You are trying to open different > files than you think (most likely in the current directory). > > Kind regards > robert Many Thanks for replying robert, It makes sense what you have said. I think it would be a lot easier if I just explain my end goal. I think my approach is probably wrong. This is what I want to achieve, "Processing every single file that is in a particular directory" My current approach is , 1. Getting all files from a Directory using ab =Dir.open("K:/test/").entries 2. Iterating over each file that is in the directory by doing; ab.each do |f| 3. Applying "File.open" method (with required permission) to every single 'file' by doing, ab.each do |f| File.Open(f,"w+") do |readfile| 4. Finally manipulating/processing each file line by line. But what you said makes sense it looks like Dir.Open().entries doesn't return a "file object" that can be manipulated it rather returns a string (file name only). Do you have any idea how this can be addressed? Sorry to be a pain... new to Ruby! Many Thanks, -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 4 Apr 2010 13:03 On 04.04.2010 18:25, Hawksury Gear wrote: >> Your error is most likely caused by the fact that Dir#entries returns >> file names only, i.e. without a path. You are trying to open different >> files than you think (most likely in the current directory). >> >> Kind regards >> robert > > Many Thanks for replying robert, It makes sense what you have said. > I think it would be a lot easier if I just explain my end goal. I think > my approach is probably wrong. This is what I want to achieve, > > "Processing every single file that is in a particular directory" > > My current approach is , > > 1. Getting all files from a Directory using ab > =Dir.open("K:/test/").entries You should close the Dir object properly. You can either use the block form of Dir.open or use the approach I have used below. > 2. Iterating over each file that is in the directory by doing; > ab.each do |f| > 3. Applying "File.open" method (with required permission) to every > single 'file' by doing, > ab.each do |f| > File.Open(f,"w+") do |readfile| > > 4. Finally manipulating/processing each file line by line. > > But what you said makes sense it looks like Dir.Open().entries doesn't > return a "file object" that can be manipulated it rather returns a > string (file name only). > Do you have any idea how this can be addressed? There are different ways. You can create proper file names with the tools you have already: dir = "K:/test" Dir.entries(dir).each do |file| path = File.join dir, file if File.file? path File.open path do |io| io.each_line do |line| ... end end end end Or you can use the elegant Pathname library: require 'pathname' dir = Pathname "K:/test" dir.entries.each do |file| if file.file? file.each_line do |line| ... end end end > Sorry to be a pain... new to Ruby! No need to worry. We all start out as beginners at some point in time. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Robert Klemme on 4 Apr 2010 13:12 On 04.04.2010 19:03, Robert Klemme wrote: > Or you can use the elegant Pathname library: Even better require 'pathname' dir = Pathname "K:/test" dir.each_entry do |file| if file.file? file.each_line do |line| ... end end end Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Hawksury Gear on 4 Apr 2010 13:58
> Even better > > require 'pathname' > dir = Pathname "K:/test" > > dir.each_entry do |file| > if file.file? > file.each_line do |line| > ... > end > end > end > > Cheers > robert Many Thanks Robert, I did the following, require 'pathname' dir= Pathname "K:/test" dir.each_entry do |file| if file.file? file.each {|line| print line} ## Checking if it can access a line in a file end end It isn't giving any error message which is great but also not Displaying any Output. Probably i am not using file.each{|line| print line} correctly. Thanks, Gear -- Posted via http://www.ruby-forum.com/. |