Can't delete a file

1508
5
Jump to solution
08-23-2012 06:43 AM
DaveJordan1
New Contributor III
My script creates temporary text files (name.tmp).  when I am done with them, I want to delete them but there is a lock on file and it won't delete.  I am closing the file after using it. 

How can I force the files to be closed so I can delete them?
OR
What I am doing wrong?

  arcpy.AddMessage("Writing records to Owner List...")   f1 = open(oName,'w')   #  Write the record header   f1.write("OWNER1|OWNER2|MAIL1|MAIL2|CITY_STATE_ZIP5_ZIP4" + "\n")   rows = arcpy.SearchCursor(ParcelLayer)  #(NotifyLyr)  #(ParcelLayer)   cnt = 0   LabelCnt = 0   PageCnt = 1   f1.write(str(DirName+"|Page"+(str(PageCnt))+"| | |") + "\n")   f1.write(str(AppName+"|"+AppName2+"|"+AppStreet+"|"+AppCityZip) + "\n")   for row in rows:       cnt = cnt + 1       LabelCnt = LabelCnt + 1       f1.write(str(row.getValue("OwnerName"))    + "|" + str(row.getValue("SecondName"))    + "|" + str(row.getValue("MailHouseNo"))+ " " + str(row.getValue("MailAddressLine1"))    + "|" + str(row.getValue("MailAddressLine2"))    + "|" + str(row.getValue("MailCity"))+ " " + str(row.getValue("MailState")) + " " + str(row.getValue("MailZip5"))     + " " + str(row.getValue("MailZip4")).replace("|None|","| |")    + "\n")           if LabelCnt == 29:         PageCnt = PageCnt + 1         f1.write(str(DirName+"|Page"+(str(PageCnt))+"| | |") + "\n")         LabelCnt = 0   f1.close()   del row, rows # Some records reflect a null vale as |None|, replace these with a blank   FoName = wPath+DirName+"\\"+DirName+"-o.txt"   f1 = open(oName,'r')   f2 = open(FoName, 'w')   lines = f1.readlines()   for line in lines:  f2.write(line.replace("None"," "))   f1.close   f2.close   arcpy.Delete_management(oName)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
  What I am doing wrong?


Close is a function. To make it work you need parentheses (you're missing them at the end)

>>> f1.close  # just prints the string rep of the object <built-in method close of file object at 0x01E55E90> >>> f1.close()  # with the parens the function will actually do the close


I'm a huge fan of Python's interactive window. FORTRAN sure didn't have that. 🙂

View solution in original post

0 Kudos
5 Replies
markdenil
Occasional Contributor III
try os.remove(path)
instead of
arcpy.Delete_management(oName)
0 Kudos
DaveJordan1
New Contributor III
I have tried using the os.remove and os.unlink , I get similar results.  It's like the file is n't being closed.  here is the result of os.remove:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: u'Y:\\Notification Radius Pkgs\\\\dave6\\dave6-o.tmp'
[Error 32] The process cannot access the file because it is being used by another process: u'Y:\\Notification Radius Pkgs\\\\dave6\\dave6-o.tmp'
0 Kudos
ChristopherThompson
Occasional Contributor III
if you are using the os.remove method to do this, try wrapping those statements in a 'try:... except:' block.  This may allow the remove to get rid of everything other than the lock files, and then once those are gone and your code exits, the locks should delete on their own.  I think this is how I've approached this in the past and seems to work.
0 Kudos
curtvprice
MVP Esteemed Contributor
  What I am doing wrong?


Close is a function. To make it work you need parentheses (you're missing them at the end)

>>> f1.close  # just prints the string rep of the object <built-in method close of file object at 0x01E55E90> >>> f1.close()  # with the parens the function will actually do the close


I'm a huge fan of Python's interactive window. FORTRAN sure didn't have that. 🙂
0 Kudos
DaveJordan1
New Contributor III
ahhh - Sometimes it is the things that should be obvious that make it sooo difficult to find. Adding the parenthesis seems to have solved it. Thank you

f1.close()
0 Kudos