deleting os files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Aherne

    deleting os files

    Being a bit of a newbie, I hope this question isn't too stupid. I have
    searched the archives and docs for any reports about files and csv
    messages and not found anything that mentions the problem I am having.

    I have happily been using the os.remove and file.close commands with a
    program that sends data to an ftp site.

    Now I am using the csv module to read some os files and extract some
    fields that I use to send messages to someone.

    Once the message has been sent I want to delete the file. Great I
    think. I'll cannibalise the code I did for the ftp stuff.

    The program runs in a permanent while loop and uses a for loop to
    process files from a particular directory.

    Unfortunately, when it comes to delete, I get a permission error. And
    yes, when I run the program and try to delete the file from the
    command prompt, I get a permission error.

    As far as I can tell, I assume the file is being kept open in the
    DictReader call. But I can't work out how I can close the file -
    ..close comand also returns an error. I was assuming that on the return
    from the call the file would be closed - mistakenly I now think.

    This is using python 2.3.3 on W2k

    The sample code is below. Not much to it. If I comment out the close
    and delete lines (not shown here) then the program happily carries on
    processing the same files over and over. Not what I want.

    I hope the code shows up in a rerasonable format for reading.

    locallist = [(x) for x in os.listdir(loca ldir) if
    os.path.getsize (os.path.join(l ocaldir, x)) > 0] # upload all local
    files
    localfiles = fnmatch.filter( locallist, '*.OUT')
    for localname in localfiles:
    localpath = os.path.join(lo caldir, localname)
    if localname[-3:] == 'OUT':
    csv_file = file(localpath)
    reader = csv.DictReader( csv_file,['phone', 'msg', 'PZ', 'MP'])
    for row in reader:
    phone = row['phone']
    msg = row['msg']

    print time.ctime(time .time()),' uploading', msg, 'to',
    phone

    if anyone can point me in the right direction I would much appreciate
    it.
  • Peter Hansen

    #2
    Re: deleting os files

    John Aherne wrote:
    ....[color=blue]
    > Unfortunately, when it comes to delete, I get a permission error. And
    > yes, when I run the program and try to delete the file from the
    > command prompt, I get a permission error.[/color]

    Usually posting actual tracebacks, or the actual error messages
    from your console, is a very good idea. One man's permission
    error might be another's missing file, to paraphrase an old
    aphorism.
    [color=blue]
    > As far as I can tell, I assume the file is being kept open in the
    > DictReader call. But I can't work out how I can close the file -
    > .close comand also returns an error. I was assuming that on the return
    > from the call the file would be closed - mistakenly I now think.[/color]

    I don't believe csv Readers ever touch the file other than to read
    from it. But they do cache a reference to it, meaning that the
    automatic closure which you've perhaps become used to from other
    uses of files is no longer happening. Deleting the Reader itself
    might work, but it's not the "best" solution.
    [color=blue]
    > The sample code is below. Not much to it. If I comment out the close
    > and delete lines (not shown here) then the program happily carries on
    > processing the same files over and over. Not what I want.[/color]

    Unfortunately, without those lines, and without some idea of where
    "csv_file" is being created/opened, the code isn't much help.

    Please either post a more useful sample, or experiment with this:
    maintain a reference to the csv_file object (I guess you have it
    right there, but since I can't see where you opened that object
    I don't know for sure what it actually is), and after you are
    done reading from it, preferably in the "finally" stanza of a
    try/finally block, do a csv_file.close( ). This "should" work,
    but without seeing more of your code it's really only a guess
    on my part.

    -Peter

    Comment

    • Kent Johnson

      #3
      Re: deleting os files

      John Aherne wrote:[color=blue]
      > Unfortunately, when it comes to delete, I get a permission error. And
      > yes, when I run the program and try to delete the file from the
      > command prompt, I get a permission error.
      >
      > As far as I can tell, I assume the file is being kept open in the
      > DictReader call. But I can't work out how I can close the file -
      > .close comand also returns an error. I was assuming that on the return
      > from the call the file would be closed - mistakenly I now think.[/color]
      [color=blue]
      > locallist = [(x) for x in os.listdir(loca ldir) if
      > os.path.getsize (os.path.join(l ocaldir, x)) > 0] # upload all local
      > files
      > localfiles = fnmatch.filter( locallist, '*.OUT')
      > for localname in localfiles:
      > localpath = os.path.join(lo caldir, localname)
      > if localname[-3:] == 'OUT':
      > csv_file = file(localpath)
      > reader = csv.DictReader( csv_file,['phone', 'msg', 'PZ', 'MP'])
      > for row in reader:
      > phone = row['phone']
      > msg = row['msg']
      >
      > print time.ctime(time .time()),' uploading', msg, 'to',
      > phone[/color]

      csv_file.close( ) # after you finish reading the rows

      Kent

      Comment

      • John Aherne

        #4
        Re: deleting os files

        Kent Johnson <kent3737@yahoo .com> wrote in message news:<41a9f352$ 1_3@newspeer2.t ds.net>...[color=blue]
        > John Aherne wrote:[color=green]
        > > Unfortunately, when it comes to delete, I get a permission error. And
        > > yes, when I run the program and try to delete the file from the
        > > command prompt, I get a permission error.
        > >
        > > As far as I can tell, I assume the file is being kept open in the
        > > DictReader call. But I can't work out how I can close the file -
        > > .close comand also returns an error. I was assuming that on the return
        > > from the call the file would be closed - mistakenly I now think.[/color]
        >[color=green]
        > > locallist = [(x) for x in os.listdir(loca ldir) if
        > > os.path.getsize (os.path.join(l ocaldir, x)) > 0] # upload all local
        > > files
        > > localfiles = fnmatch.filter( locallist, '*.OUT')
        > > for localname in localfiles:
        > > localpath = os.path.join(lo caldir, localname)
        > > if localname[-3:] == 'OUT':
        > > csv_file = file(localpath)
        > > reader = csv.DictReader( csv_file,['phone', 'msg', 'PZ', 'MP'])
        > > for row in reader:
        > > phone = row['phone']
        > > msg = row['msg']
        > >
        > > print time.ctime(time .time()),' uploading', msg, 'to',
        > > phone[/color]
        >
        > csv_file.close( ) # after you finish reading the rows
        >
        > Kent[/color]

        Thanks for the responses.

        I had tried the csv_file.close( ) but was getting an exception in my
        try/except block.

        I reckon I'm doing this wrong or in the wrong place. So I'll try
        reworking the code and see where I get.

        At least I can forget about the DictReader class. It's my code I need
        to look at.

        Thanks

        John

        Comment

        Working...