<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can't delete a file in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90868#M7087</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;try os.remove(path)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;instead of &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.Delete_management(oName)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 23 Aug 2012 15:02:29 GMT</pubDate>
    <dc:creator>markdenil</dc:creator>
    <dc:date>2012-08-23T15:02:29Z</dc:date>
    <item>
      <title>Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90867#M7086</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My script creates temporary text files (name.tmp).&amp;nbsp; when I am done with them, I want to delete them but there is a lock on file and it won't delete.&amp;nbsp; I am closing the file after using it.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How can I force the files to be closed so I can delete them?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;OR&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What I am doing wrong?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;PRE class="plain" name="code"&gt;&amp;nbsp; arcpy.AddMessage("Writing records to Owner List...") &amp;nbsp; f1 = open(oName,'w') &amp;nbsp; #&amp;nbsp; Write the record header &amp;nbsp; f1.write("OWNER1|OWNER2|MAIL1|MAIL2|CITY_STATE_ZIP5_ZIP4" + "\n") &amp;nbsp; rows = arcpy.SearchCursor(ParcelLayer)&amp;nbsp; #(NotifyLyr)&amp;nbsp; #(ParcelLayer) &amp;nbsp; cnt = 0 &amp;nbsp; LabelCnt = 0 &amp;nbsp; PageCnt = 1 &amp;nbsp; f1.write(str(DirName+"|Page"+(str(PageCnt))+"| | |") + "\n") &amp;nbsp; f1.write(str(AppName+"|"+AppName2+"|"+AppStreet+"|"+AppCityZip) + "\n") &amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt = cnt + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LabelCnt = LabelCnt + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f1.write(str(row.getValue("OwnerName")) &amp;nbsp;&amp;nbsp; + "|" + str(row.getValue("SecondName")) &amp;nbsp;&amp;nbsp; + "|" + str(row.getValue("MailHouseNo"))+ " " + str(row.getValue("MailAddressLine1")) &amp;nbsp;&amp;nbsp; + "|" + str(row.getValue("MailAddressLine2")) &amp;nbsp;&amp;nbsp; + "|" + str(row.getValue("MailCity"))+ " " + str(row.getValue("MailState")) + " " + str(row.getValue("MailZip5"))&amp;nbsp; &amp;nbsp;&amp;nbsp; + " " + str(row.getValue("MailZip4")).replace("|None|","| |") &amp;nbsp;&amp;nbsp; + "\n") &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if LabelCnt == 29: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PageCnt = PageCnt + 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; f1.write(str(DirName+"|Page"+(str(PageCnt))+"| | |") + "\n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LabelCnt = 0 &amp;nbsp; f1.close() &amp;nbsp; del row, rows # Some records reflect a null vale as |None|, replace these with a blank &amp;nbsp; FoName = wPath+DirName+"\\"+DirName+"-o.txt" &amp;nbsp; f1 = open(oName,'r') &amp;nbsp; f2 = open(FoName, 'w') &amp;nbsp; lines = f1.readlines() &amp;nbsp; for line in lines:&amp;nbsp; f2.write(line.replace("None"," ")) &amp;nbsp; f1.close &amp;nbsp; f2.close &amp;nbsp; arcpy.Delete_management(oName)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 13:43:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90867#M7086</guid>
      <dc:creator>DaveJordan1</dc:creator>
      <dc:date>2012-08-23T13:43:03Z</dc:date>
    </item>
    <item>
      <title>Re: Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90868#M7087</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;try os.remove(path)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;instead of &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.Delete_management(oName)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Aug 2012 15:02:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90868#M7087</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-08-23T15:02:29Z</dc:date>
    </item>
    <item>
      <title>Re: Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90869#M7088</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have tried using the os.remove and os.unlink , I get similar results.&amp;nbsp; It's like the file is n't being closed.&amp;nbsp; here is the result of os.remove:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[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'&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Aug 2012 12:50:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90869#M7088</guid>
      <dc:creator>DaveJordan1</dc:creator>
      <dc:date>2012-08-24T12:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90870#M7089</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;if you are using the os.remove method to do this, try wrapping those statements in a 'try:... except:' block.&amp;nbsp; 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.&amp;nbsp; I think this is how I've approached this in the past and seems to work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Aug 2012 15:08:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90870#M7089</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2012-08-24T15:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90871#M7090</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&amp;nbsp; What I am doing wrong? &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Close is a function. To make it work you need parentheses (you're missing them at the end)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;gt;&amp;gt;&amp;gt; f1.close&amp;nbsp; # just prints the string rep of the object &amp;lt;built-in method close of file object at 0x01E55E90&amp;gt; &amp;gt;&amp;gt;&amp;gt; f1.close()&amp;nbsp; # with the parens the function will actually do the close&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm a huge fan of Python's interactive window. FORTRAN sure didn't have that. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Aug 2012 15:38:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90871#M7090</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-08-24T15:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Can't delete a file</title>
      <link>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90872#M7091</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;f1.close()&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Aug 2012 12:27:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-delete-a-file/m-p/90872#M7091</guid>
      <dc:creator>DaveJordan1</dc:creator>
      <dc:date>2012-08-27T12:27:11Z</dc:date>
    </item>
  </channel>
</rss>

