unable to delete a file

Discussion in 'Perl Misc' started by cartercc, Feb 5, 2009.

  1. cartercc

    cartercc Guest

    The script downloads a file named EAUSR.txt from a remote server. The
    contents of the file are new each day, that is, I always have to
    download a new EAUSR.txt in order to process the new data. After I
    process the data, I need to delete EAUSR.txt to prepare for the next
    day's download.

    Here is the code I use:

    1 $file = get_daily_file(); # gets EAUSR.txt
    2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
    3 process_daily_file(); # calls a number of different subs
    4 close INFILE;
    5 unlink $file; # does not delete EAUSR.txt
    6 exit();

    Here's the problem: After the script exits, EAUSR.txt stil exists. If
    I move line 5 to the beginning of the script, it works fine --
    EAUSR.txt is deleted before I get each day's new file. The funny thing
    is that my script has run for years deleting the file at the end, as
    shown above, but a couple of days ago, I made some extensive revisions
    to the script and now it doesn't work.

    It's not a permissions problem. It's not because INFILE is still open
    (I don't think). It's not because unlink doesn't work. I don't have
    any idea why unlink doesn't work at the end, but works at the
    beginning. Any ideas?

    Question: is there any version of close that closes all open
    filehandles? The script as a whole is about 500 lines long, and in
    running opens and closes dozens of files, and I'm thinking that maybe
    the reason the file isn't deleted is because I have inadvertently not
    closed it, although I have paired up all calls to open with a call to
    close.

    TIA, CC.
    cartercc, Feb 5, 2009
    #1
    1. Advertising

  2. cartercc <> wrote:

    > 5 unlink $file; # does not delete EAUSR.txt



    Ask it why it failed by checking the return value and including $!

    unlink $file or die "could not delete '$file' $!";


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 5, 2009
    #2
    1. Advertising

  3. cartercc wrote:
    >
    > 1 $file = get_daily_file(); # gets EAUSR.txt
    > 2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
    > 3 process_daily_file(); # calls a number of different subs
    > 4 close INFILE;
    > 5 unlink $file; # does not delete EAUSR.txt
    > 6 exit();
    >
    > Here's the problem: After the script exits, EAUSR.txt stil exists.


    ....

    > ... a couple of days ago, I made some extensive revisions
    > to the script and now it doesn't work.
    >
    > It's not a permissions problem. It's not because INFILE is still open
    > (I don't think).


    How about letting Perl help you check the latter?

    close INFILE or die $!;

    > It's not because unlink doesn't work.


    How about ensuring that that's the case?

    unlink $file or die $!;

    > Question: is there any version of close that closes all open
    > filehandles? The script as a whole is about 500 lines long, and in
    > running opens and closes dozens of files, and I'm thinking that maybe
    > the reason the file isn't deleted is because I have inadvertently not
    > closed it,


    If that's the case, the error message resulting from the failed
    unlink()ing ought to tell you.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Feb 5, 2009
    #3
  4. cartercc

    Tim Greer Guest

    cartercc wrote:

    > 5 unlink $file;                  # does not delete EAUSR.txt


    It could be anything from the mount options on the drive, to file
    attribute settings, to anything else. Check the return value and the
    error if it fails, like you would on an open call unlink $file or die
    "can't unlike $file $!";


    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Feb 5, 2009
    #4
  5. cartercc

    cartercc Guest

    On Feb 5, 12:48 pm, Tad J McClellan <> wrote:
    > cartercc <> wrote:
    > > 5 unlink $file;                  # does not delete EAUSR.txt

    >
    > Ask it why it failed by checking the return value and including $!
    >
    >    unlink $file or die "could not delete '$file' $!";
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


    Funny. I just tried this, and unlink deleted the EAUSR file just like
    it was supposed to. Did not get any error message at all.

    Must be the McClellan hex -- die or else!

    CC
    cartercc, Feb 5, 2009
    #5
  6. cartercc <> wrote:
    > On Feb 5, 12:48 pm, Tad J McClellan <> wrote:
    >> cartercc <> wrote:
    >> > 5 unlink $file;                  # does not delete EAUSR.txt

    >>
    >> Ask it why it failed by checking the return value and including $!
    >>
    >>    unlink $file or die "could not delete '$file' $!";



    > Funny. I just tried this, and unlink deleted the EAUSR file just like
    > it was supposed to. Did not get any error message at all.
    >
    > Must be the McClellan hex -- die or else!



    You have the order of the operands reversed. It's more like:

    Do what I ask or die!

    :)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 5, 2009
    #6
  7. cartercc

    cartercc Guest

    On Feb 5, 2:08 pm, Tad J McClellan <> wrote:
    > You have the order of the operands reversed. It's more like:
    >
    >    Do what I ask or die!
    >
    > :)


    Yeah, but it's funnier the way I wrote it, as if dying is the best
    option, not the worst. What other options do you have if the first one
    is die?

    Your way is the same as 'your money or your life' which is more of a
    dog-bites-man story rather than a man-bites-dog story.
    cartercc, Feb 5, 2009
    #7
  8. cartercc

    Tim Greer Guest

    cartercc wrote:

    > On Feb 5, 2:08 pm, Tad J McClellan <> wrote:
    >> You have the order of the operands reversed. It's more like:
    >>
    >> Do what I ask or die!
    >>
    >> :)

    >
    > Yeah, but it's funnier the way I wrote it, as if dying is the best
    > option, not the worst. What other options do you have if the first one
    > is die?


    So, don't use die, use something else relevant and helpful to report
    failures (or log them), or whatever it is you want to do in said
    situation.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
    Tim Greer, Feb 5, 2009
    #8
  9. On 2009-02-05, cartercc <> wrote:
    *SKIP*
    > 1 $file = get_daily_file(); # gets EAUSR.txt
    > 2 open INFILE, "<", $file or die "Cannot open INFILE, $!";
    > 3 process_daily_file(); # calls a number of different subs
    > 4 close INFILE;
    > 5 unlink $file; # does not delete EAUSR.txt
    > 6 exit();

    *SKIP*
    > It's not a permissions problem. It's not because INFILE is still open
    > (I don't think). It's not because unlink doesn't work. I don't have
    > any idea why unlink doesn't work at the end, but works at the
    > beginning. Any ideas?


    perl -wle '
    my $fn = q|foo|;
    print q|one: |, -e $fn ? 1 : 0;
    open my $fh, q|>|, $fn or die $!;
    print q|two: |, -e $fn ? 1 : 0;
    unlink $fn or die $!;
    print q|three: |, -e $fn ? 1 : 0;
    close $fh;
    print q|four: |, -e $fn ? 1 : 0;
    '
    one: 0
    two: 1
    three: 0
    four: 0

    So the question is: what's your platform?

    *CUT*

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
    Eric Pozharski, Feb 6, 2009
    #9
  10. cartercc wrote:
    > On Feb 5, 12:48 pm, Tad J McClellan <> wrote:
    >> cartercc <> wrote:
    >>> 5 unlink $file; # does not delete EAUSR.txt

    >> Ask it why it failed by checking the return value and including $!
    >>
    >> unlink $file or die "could not delete '$file' $!";
    >>
    >> --
    >> Tad McClellan
    >> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

    >
    > Funny. I just tried this, and unlink deleted the EAUSR file just like
    > it was supposed to. Did not get any error message at all.


    It looks as if you had a bug in the original program which was fixed
    when you added the "or die ..." part.

    Not unusual.

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Feb 6, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guru Prasad
    Replies:
    1
    Views:
    449
    Ken Cox [Microsoft MVP]
    Jul 25, 2003
  2. Robert Rotstein
    Replies:
    2
    Views:
    1,242
    Bruce Barker
    Dec 13, 2005
  3. Ramiro Barbosa, Jr.

    Unable to delete [] char* in MSVC 6.0

    Ramiro Barbosa, Jr., Oct 8, 2004, in forum: C++
    Replies:
    2
    Views:
    508
    Sharad Kala
    Oct 8, 2004
  4. Replies:
    3
    Views:
    8,344
  5. Jake Barnes
    Replies:
    6
    Views:
    377
    9.co.uk
    Feb 8, 2006
Loading...

Share This Page