By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

deleting os files

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
Kent Johnson <ke******@yahoo.com> wrote in message news:<41**********@newspeer2.tds.net>...
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


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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.