# File deletion failed -- how to handle problem?

**URL:** <https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306>\
**Category:** Ruby\
**Created:** [March 30, 2006, 7:04am UTC](https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306 "2006-03-30T07:04:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [March 30, 2006, 7:04am UTC](https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306/1 "2006-03-30T07:04:18Z")

</div>

Hi All,

I wrote a simple test a techiques I’ll apply in an app I’m going start  
shortly. One thing my test did is: – -- open a non-existent file for  
appending,  
– populate it  
– close it  
– open it for reading, expecting automatic closure when reading ended  
– looped through file printing its line to STDOUT  
– attempted to delete the file – got “Permission denied -  
TestOutput.txt (Errno::EACCES)”

I tested it under SciTE.

The relevant code fragment is listed below. I think the problem is  
that the file is in the process of being closed by WindowsXP-Pro when  
the delete request hits the OS and the OS complains.

If that’s correct, all I have to do is write some loop that naps and  
re-attempts the deletion until successful. Is there better approach,  
or is my analysis wrong in the first place?

\*\*\*\*\*\*\*\*\* Code fragment \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
puts “\nDisplaying the content of #{fno}:”  
puts “Opening #{fno}’”  
File.open(fno, “r”) do |file| # Open for reading  
while sLine = file.gets  
puts sLine  
end  
end # File will be closed automantically when scope of “do” exited  
puts “#{fno} closed”

puts “\nDeleting #{fno}”  
File.delete(fno)

* * *

TIA,  
Richard

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [March 31, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306/2 "2006-03-31T13:21:53Z")

</div>

Richard schrieb:

> …  
> Richard, your code works for me, when I run it from the command line. I  
> haven’t tried it with SciTE.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [March 31, 2006, 4:50pm UTC](https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306/3 "2006-03-31T16:50:51Z")

</div>

Worked for me:

ruby 1.8.2 (2004-12-25) [i386-mswin32]  
SciTE Version 1.59

---

<div class="post-metadata">

**Author:** ![richard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f0a364/32.png) [@richard](https://www.ruby-forum.com/u/richard)\
**Post date:** [March 31, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/file-deletion-failed-how-to-handle-problem/93306/4 "2006-03-31T18:04:10Z")

</div>

Richard wrote:

> Hi All,

> end  
> end # File will be closed automantically when scope of “do” exited  
> puts “#{fno} closed”
> 
> puts “\nDeleting #{fno}”  
> File.delete(fno)
> 
> * * *

It looks like you’re passing around a fileno. If that’s the case, the  
implication is that you’ve opened the filehandle somewhere else, but  
haven’t closed it.

Also, you’re better off using IO.foreach(file) instead of the File.open  
block you’ve got there.

Regards,

Dan
