deleting os files

Kent Johnson kent3737 at yahoo.com
Sun Nov 28 10:54:23 EST 2004


John Aherne wrote:
> 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.

>     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

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

Kent



More information about the Python-list mailing list