deleting os files

John Aherne johnaherne at rocs.co.uk
Sun Nov 28 08:00:08 EST 2004


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(localdir) if   
os.path.getsize(os.path.join(localdir, x)) > 0]     # upload all local
files
    localfiles = fnmatch.filter(locallist, '*.OUT')
    for localname in localfiles:  
       localpath = os.path.join(localdir, 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.



More information about the Python-list mailing list