AnsweredAssumed Answered

Can't delete a file

Question asked by DaveJordan on Aug 23, 2012
Latest reply on Aug 27, 2012 by DaveJordan
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)

Outcomes