deleting os files

J

John Aherne

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.
 
P

Peter Hansen

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.

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.
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.

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.
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.

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
 
K

Kent Johnson

John said:
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
 
J

John Aherne

Kent Johnson said:
John said:
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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top