You can test whether a file is empty with -z, and -s will return a size (so you can use it to test for "non-empty").

if ( -s $infile ) { my $parser = Text::CSV::Simple->new; my @data = $parser->read_file($infile); }

Or...

if ( -z $infile ) { unlink $infile or die "Can't unlink empty file '$infile': $!"; next FILE; }

Update: If the files you have are not literally zero size, you may have to stick your parsing in an eval to catch the error and deal with it at that point.

eval { my $parser = Text::CSV::Simple->new; my @data = $parser->read_file($infile); }; # don't forget the semicolon! if ( $@ ) { if ( $@ =~ /the error you expect/ ) { unlink $infile; next FILE; } # unexpected error! die $@; }

Have a look at perlvar for documentation on $@ (aka $EVAL_ERROR, if you use English).


In reply to Re: How to delete empty files by kyle
in thread How to delete empty files by padawan_linuxero

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.