File.delete -> Permission denied

  • Thread starter Antoine De Groote
  • Start date
A

Antoine De Groote

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:in `foreach'
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
 
R

Robert Sheehan

Antoine said:
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:in `foreach'
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).
 
S

Stefano Crocco

Alle domenica 8 aprile 2007, Antoine De Groote ha scritto:
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:in `foreach'
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

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
 
C

ChrisKaelin

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:in `foreach'
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

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?
 
A

Antoine De Groote

Thanks everybody,

'.' and '..' effectively where the source of the problem.

Kind regards,
antoine
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top