File.delete -> Permission denied

Hello,

I’m new to Ruby (coming from Python) and I already have trouble.

The following code (on Windows XP, Ruby 1.8.5):

Dir.chdir ‘c:/documents and settings/antoine/desktop’
Dir.mkdir ‘test’
Dir.chdir ‘test’
%w{ a b c }.each { |f| File.open(f+’.txt’, ‘w’).close }
Dir.foreach(’.’) { |f| File.delete f }

throws this error message:

problem.rb:5:in delete': Permission denied - . (Errno::EACCES) from problem.rb:5 from problem.rb:5:inforeach’
from problem.rb:5

I’ve tried to find an answer to this strange behaviour for a couple of
hours now, but I can’t find one. Could anybody point me to a solution of
the problem?

Kind regards,
antoine

Alle domenica 8 aprile 2007, Antoine De Groote ha scritto:

Dir.foreach(’.’) { |f| File.delete f }
the problem?

Kind regards,
antoine

I can see a problem in your code, but I’m not sure whether or not it’s
related
to the error you get (I don’t have use windows, so I can’t test).
Dir.foreach
passes to the block also ‘.’ and ‘…’, which represent respectively the
current directory and its parent. File.delete can’t deal with
directories
(and at any rate, I don’t think you want to delete the current
directory), so
it raises an exception. On linux, trying to use File.delete on a
directory
raises a Errno::EISDIR exception, but since exceptions in the Errno
module ar
operating system-dependent, it may be that in windows it raises a
Errno::EACCES exception.

I hope this helps

Stefano

Antoine De Groote wrote:

problem.rb:5:in delete': Permission denied - . (Errno::EACCES) from problem.rb:5 from problem.rb:5:inforeach’
from problem.rb:5

Dir.foreach also returns ‘.’ and ‘…’.
The error message is saying you can’t delete the current directory (it
still contains files).

On 8 Apr., 21:36, Antoine De Groote [email protected] wrote:

Dir.foreach(‘.’) { |f| File.delete f }
the problem?

Kind regards,
antoine

I had the same problem with the logfiles on my script-framework. They
just could not be deleted on windows, until I tried to close them
explicitly after any usage. I don’t know windows file-locking
mechanism, but closing any open file(handles) will save you troubles
anyway.
In your code above, you are doing a close, but right after opening,
maybe that’s a problem?

Thanks everybody,

‘.’ and ‘…’ effectively where the source of the problem.

Kind regards,
antoine