[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Deleting a file - is there a less clumsy way to do this?

Ronald Fischer

6/4/2007 1:15:00 PM

In my application, I often have blocks of code, where during preparation
code, I need to make sure that certain files do not exist (in practice,
this might be files left over from a previous run, which I didn't want
to have erased earlier). Basically, I am doing something like this:

if File.exist?(filename)
File.unlink(filename)
end

or, equivalently,

begin
File.unlink(filename)
rescue
# ignore errors - it's OK if the file does not exist
end

This is necessary, because File::unlink signals non-existence of the
file
using an exception, instead by return code (as, for example, Perl's
unlink
does). The resulting code looks clumsy, because one always has to take
care
of an exception, which does not really signal an error condition (in
Perl,
I would simply ignore the return code of unlink).

Of course I could write my own unlink function like this:

def silent_unlink(f)
File.unlink(f) if File.exist?(f)
end
...
silent_unlink(filename)

but I am wondering whether this is not a such common problem, that Ruby
already might have a non-exception-throwing unlink function built in
somewhere?

Ronald
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162


11 Answers

Daniel Lucraft

6/4/2007 1:18:00 PM

0

Ronald Fischer wrote:

> but I am wondering whether this is not a such common problem, that Ruby
> already might have a non-exception-throwing unlink function built in
> somewhere?
>
> Ronald

FileUtils.rm_f(filename) ?

best,
Dan

--
Posted via http://www.ruby-....

Brian Candler

6/4/2007 2:09:00 PM

0

On Mon, Jun 04, 2007 at 10:14:49PM +0900, Ronald Fischer wrote:
> In my application, I often have blocks of code, where during preparation
> code, I need to make sure that certain files do not exist (in practice,
> this might be files left over from a previous run, which I didn't want
> to have erased earlier). Basically, I am doing something like this:
>
> if File.exist?(filename)
> File.unlink(filename)
> end
>
> or, equivalently,
>
> begin
> File.unlink(filename)
> rescue
> # ignore errors - it's OK if the file does not exist
> end
>
> This is necessary, because File::unlink signals non-existence of the
> file
> using an exception, instead by return code (as, for example, Perl's
> unlink
> does). The resulting code looks clumsy, because one always has to take
> care
> of an exception, which does not really signal an error condition (in
> Perl,
> I would simply ignore the return code of unlink).

There's

File.unlink(filename) rescue nil

However this catches everything under StandardError (I think), including
NoMethodError. So if you type

File.unlikn(filename) rescue nil

then you won't see the typo.

> Of course I could write my own unlink function like this:
>
> def silent_unlink(f)
> File.unlink(f) if File.exist?(f)
> end
> ...
> silent_unlink(filename)

FYI, that code has a "race" - the existence of a file may change between the
test and the unlink. So this code *may* raise an exception, very
occasionally.

You may think this isn't a realistic problem, but race conditions which
cause crashes once in a blue moon are extremely hard to pin down. So it's a
good aim to try and write code without races if at all possible.

It's also more efficient to try to remove the file, rather than to test for
its existence (stat) *and* then try to remove it afterwards.

Regards,

Brian.

Jamey Cribbs

6/4/2007 2:20:00 PM

0

Ronald Fischer wrote:
> In my application, I often have blocks of code, where during preparation
> code, I need to make sure that certain files do not exist (in practice,
> this might be files left over from a previous run, which I didn't want
> to have erased earlier). Basically, I am doing something like this:
>
> if File.exist?(filename)
> File.unlink(filename)
> end
>

You can get it down to one line by doing this:

File.delete(filename) if File.exist?(filename)


That doesn't look so bad. :-)


Jamey


Ronald Fischer

6/4/2007 3:27:00 PM

0

> > Basically, I am doing something like this:
> >
> > if File.exist?(filename)
> > File.unlink(filename)
> > end
> >
>
> You can get it down to one line by doing this:
>
> File.delete(filename) if File.exist?(filename)
>
>
> That doesn't look so bad. :-)

Well, I'm not so much concerned about the *lines* of code,
but of the complexity, in particular since this variation
requires to name "filename" twice.

(side note: why does this remind me to the ongoing thread
"Introducing the "it" keyword"..... ;-)

In Perl for instance, I would simply write

unlink filename;

and implicitly void the result...


Ronald

Ronald Fischer

6/4/2007 3:29:00 PM

0

> File.unlink(filename) rescue nil
>
> However this catches everything under StandardError (I
> think), including
> NoMethodError. So if you type
>
> File.unlikn(filename) rescue nil
>
> then you won't see the typo.

Interesting (didn't know about this variation), but, as you said,
too risky, as typos would be ignored too.

> > def silent_unlink(f)
> > File.unlink(f) if File.exist?(f)
> > end
> > ...
> > silent_unlink(filename)
>
> FYI, that code has a "race" - the existence of a file may
> change between the
> test and the unlink. So this code *may* raise an exception, very
> occasionally.

Thank you for pointing this out. You are very right.

Ronald

Daniel Berger

6/4/2007 5:14:00 PM

0

On Jun 4, 9:26 am, "Ronald Fischer" <ronald.fisc...@venyon.com> wrote:
> > > Basically, I am doing something like this:
>
> > > if File.exist?(filename)
> > > File.unlink(filename)
> > > end
>
> > You can get it down to one line by doing this:
>
> > File.delete(filename) if File.exist?(filename)
>
> > That doesn't look so bad. :-)
>
> Well, I'm not so much concerned about the *lines* of code,
> but of the complexity, in particular since this variation
> requires to name "filename" twice.
>
> (side note: why does this remind me to the ongoing thread
> "Introducing the "it" keyword"..... ;-)
>
> In Perl for instance, I would simply write
>
> unlink filename;
>
> and implicitly void the result...
>
> Ronald

Note that this approach would silently fail on MS Windows if another
process had an open handle on the file in question, i.e. the file
_does_ exist, but you did not delete it.

Regards,

Dan

John Joyce

6/4/2007 5:42:00 PM

0


On Jun 4, 2007, at 12:15 PM, Daniel Berger wrote:

> On Jun 4, 9:26 am, "Ronald Fischer" <ronald.fisc...@venyon.com> wrote:
>>>> Basically, I am doing something like this:
>>
>>>> if File.exist?(filename)
>>>> File.unlink(filename)
>>>> end
>>
>>> You can get it down to one line by doing this:
>>
>>> File.delete(filename) if File.exist?(filename)
>>
>>> That doesn't look so bad. :-)
>>
>> Well, I'm not so much concerned about the *lines* of code,
>> but of the complexity, in particular since this variation
>> requires to name "filename" twice.
>>
>> (side note: why does this remind me to the ongoing thread
>> "Introducing the "it" keyword"..... ;-)
>>
>> In Perl for instance, I would simply write
>>
>> unlink filename;
>>
>> and implicitly void the result...
>>
>> Ronald
>
> Note that this approach would silently fail on MS Windows if another
> process had an open handle on the file in question, i.e. the file
> _does_ exist, but you did not delete it.
>
> Regards,
>
> Dan
>
>
so check success by checking File.exists? again...?
Or find out if the file is in use and then wait?

Daniel Berger

6/4/2007 5:59:00 PM

0

On Jun 4, 11:42 am, John Joyce <dangerwillrobinsondan...@gmail.com>
wrote:
> On Jun 4, 2007, at 12:15 PM, Daniel Berger wrote:
>
>
>
>
>
> > On Jun 4, 9:26 am, "Ronald Fischer" <ronald.fisc...@venyon.com> wrote:
> >>>> Basically, I am doing something like this:
>
> >>>> if File.exist?(filename)
> >>>> File.unlink(filename)
> >>>> end
>
> >>> You can get it down to one line by doing this:
>
> >>> File.delete(filename) if File.exist?(filename)
>
> >>> That doesn't look so bad. :-)
>
> >> Well, I'm not so much concerned about the *lines* of code,
> >> but of the complexity, in particular since this variation
> >> requires to name "filename" twice.
>
> >> (side note: why does this remind me to the ongoing thread
> >> "Introducing the "it" keyword"..... ;-)
>
> >> In Perl for instance, I would simply write
>
> >> unlink filename;
>
> >> and implicitly void the result...
>
> >> Ronald
>
> > Note that this approach would silently fail on MS Windows if another
> > process had an open handle on the file in question, i.e. the file
> > _does_ exist, but you did not delete it.
>
> > Regards,
>
> > Dan
>
> so check success by checking File.exists? again...?
> Or find out if the file is in use and then wait?- Hide quoted text -
>
> - Show quoted text -

begin
File.delete(file)
rescue Errno::EACCES
puts "There's an open handle somewhere"
raise
rescue Errno::ENOENT
# Ignore
end

Regards,

Dan

Lloyd Linklater

6/4/2007 8:38:00 PM

0

Jamey Cribbs wrote:
> You can get it down to one line by doing this:
>
> File.delete(filename) if File.exist?(filename)
>
>
> That doesn't look so bad. :-)

Very nice, Jamey!

--
Posted via http://www.ruby-....

Ronald Fischer

6/5/2007 7:55:00 AM

0

> > but I am wondering whether this is not a such common
> problem, that Ruby
> > already might have a non-exception-throwing unlink function built in
> > somewhere?
> >
> > Ronald
>
> FileUtils.rm_f(filename) ?

Thank you - this is indeed a useful solution!

Ronald
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162