Note that there are some explanatory texts on larger screens.

plurals
  1. POFile.delete throws Errno:EACCESS Permission Denied in ruby
    text
    copied!<p>The following code that is meant to delete lines that match a regular expression fails</p> <pre><code>def delete_entry(name) puts "Deleting #{name}.." if $DEBUG begin File.open("#{@file_name}.tmp", 'w') do |out_file| File.open(@file_name, 'r').each do |line| unless line =~ /^#{name},/ out_file.print line else puts "Deleted #{line}!" end end end File.delete(@file_name) File.rename("#{@file_name}.tmp", @file_name) rescue Exception puts "Exception thrown in PhoneBook::delete_entry(#{name}): #{$!}" end end </code></pre> <p>The temporary file works just fine. The corresponding entries are deleted properly. But when I try to delete the old file, and rename the tmp to the new file, File.delete throws the following: (line 56 is the call to File#delete)</p> <pre><code>Exception `Errno::EACCES' at PhoneBook.rb:56 - Permission denied - file-io-sampl es/phonebooks/test.csv Exception thrown in PhoneBook::delete_entry(Mike): Permission denied - file-io-s amples/phonebooks/test.csv </code></pre> <p>Any help would be appreciated. This is on Windows 7 using a NTFS filesystem if that helps matters.</p> <p>Edit: As per Az's suggestion in the comments I added:</p> <pre><code>ObjectSpace.each_object(File) { |f| p f if f.path == @file_name &amp;&amp; !f.closed? } </code></pre> <p>Just before the call to File.delete. The output is below:</p> <pre><code>C:\Pickaxe&gt;ruby PhoneBook.rb true Enter a phonebook! test.csv Using test.csv.. Open Called! Name: Richard Sex: Male Age: 22 Name: Midori Sex: Female Age: 22 Name: Mike Sex: Male Age: 18 Name: Richard Sex: Male Age: 44 Deleting Mike.. Deleted Mike,Male, 18 ! #&lt;File:file-io-samples/phonebooks/test.csv&gt; #&lt;File:file-io-samples/phonebooks/test.csv&gt; Exception `Errno::EACCES' at PhoneBook.rb:56 - Permission denied - file-io-sampl es/phonebooks/test.csv Exception thrown in PhoneBook::delete_entry(Mike): Permission denied - file-io-s amples/phonebooks/test.csv C:\Pickaxe&gt; </code></pre> <p>The two lines prefixed with # is the output of the ObjectSpace call.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload