473,420 Members | 4,459 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

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(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
3 1640
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MichaƂ Januszczyk | last post by:
Hello. I've got the following problem: I created a windows installer project that installs my application. The application works with some files. I want to include some example files with the...
5
by: Rosa | last post by:
Hi, I'm trying to clear the TIF on Windows XP programmatically with the below code. This code works fine on any folder but the TIF. For some reason the atEnd() statements always defaults to true...
3
by: Mike Turco | last post by:
I'm working on an application that imports and exports tons of CSV data, like 64,000 records per file, and six or seven files in a set. First off, by the tine I import a few hundred thousand...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
3
by: Martin Ho | last post by:
Can someone help me with this please? I wasn't very clear in my old post. I have a program to copy files from one location to another, now I want to copy only those files which were created on...
2
by: Josh Kandiko | last post by:
Hi, I've been having some frustrations with deleting files from the Isolated Storage directories. Basically, I want my application to remove all instances of configuration information for my...
7
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I have an application that pulls files, folders and registry keys of installed programs. I'm wanting to with a context menu selection of "Delete...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
0
by: DanInTacoma | last post by:
We have a fairly straightforward maint plan on one of our servers (MSSQL 2K)...it backs up several DB's to a folder on another server (the one with the tape drive) so they can be archived to tape. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.