# Finalbuilder not releasing filehandle after script failure

**URL:** https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167
**Category:** Bugs
**Created:** [October 23, 2009, 3:45am UTC](https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167 "2009-10-23T03:45:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![deleted\_user](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/deleted_user/32/937_2.png) [@deleted\_user](https://www.finalbuilder.com/forums/u/deleted_user)
#### Post date: [October 23, 2009, 3:45am UTC](https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167/1 "2009-10-23T03:45:55Z")

</div>

Execution of Python script is not releasing a file, so no furhter operations can be completed against it.

---

<div class="post-metadata">

### Author: ![Vincent](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/vincent/32/938_2.png) [@Vincent](https://www.finalbuilder.com/forums/u/Vincent)
#### Post date: [October 26, 2009, 12:02pm UTC](https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167/2 "2009-10-26T12:02:57Z")

</div>

Unfortunately I’m not sure there is much we can do about this. The script engine is Iron Python… which is written by microsoft. If you provide us with a sample that demonstrates the problem we can take a look and perhaps log it with them.

---

<div class="post-metadata">

### Author: ![deleted\_user](https://www.finalbuilder.com/forums/user_avatar/www.finalbuilder.com/deleted_user/32/937_2.png) [@deleted\_user](https://www.finalbuilder.com/forums/u/deleted_user)
#### Post date: [November 19, 2009, 1:42am UTC](https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167/3 "2009-11-19T01:42:28Z")

</div>

Here is a sample which is having similar issues.&nbsp; I wouldl ike to be able to delte file1 and file2 in later steps, after&nbsp;I have used them as part of a cleanup before the project closes.&nbsp; At this point the files cannot be deleted until after the FB project is closed.&nbsp; I used unlocker to determine what is locking the file and it does not clear until I close FB.

this is using the Run Script action, OnExecute (script language=Python)

import sys, re  
regex=re.compile(r'(^[\w]+\.[\w\W]+$)')

file1 = open(FBVariables["tempFile"],"r")  
file2 = open(FBVariables["revisionList"], "w")  
unique\_lines = list(set(file1.read().splitlines()))  
unique = set(file1.read().split("\n"))  
#print unique\_lines  
#print "\nsorted lines:\n"  
unique\_lines.sort()  
for item in unique\_lines:  
&nbsp;&nbsp;&nbsp; a=item.split("/")  
&nbsp;&nbsp;&nbsp; #print "last item is %s" % (a[-1])  
&nbsp;&nbsp;&nbsp; fname=regex.match(a[-1])  
&nbsp;&nbsp;&nbsp; if&nbsp; fname is not None:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #print "match on item %s" % (item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file2.write("".join(item + "\n"))  
&nbsp;&nbsp;&nbsp; #else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #print "NOT including item: %s"&nbsp; % (item)  
file1.close()  
file2.close()

---

<div class="post-metadata">

### Author: ![paulsamways](https://www.finalbuilder.com/forums/letter_avatar_proxy/v4/letter/p/e8c25b/32.png) [@paulsamways](https://www.finalbuilder.com/forums/u/paulsamways)
#### Post date: [November 23, 2009, 1:46am UTC](https://www.finalbuilder.com/forums/t/finalbuilder-not-releasing-filehandle-after-script-failure/3167/4 "2009-11-23T01:46:00Z")

</div>

Hi Lee,   
  
Wrapping the code between opening the file and closing it within a Try...Finally block should fix the problem you're having, for example:

&nbsp;

import sys, re

regex=re.compile(r'(^[\w]+\.[\w\W]+$)')

&nbsp;

file1 = open(FBVariables["tempFile"],"r")

try:

file2 = open(FBVariables["revisionList"], "w")

try:

unique\_lines = list(set(file1.read().splitlines()))

unique = set(file1.read().split("\n"))

#print unique\_lines

#print "\nsorted lines:\n"

unique\_lines.sort()

for item in unique\_lines:

a=item.split("/")

#print "last item is %s" % (a[-1])

fname=regex.match(a[-1])

if &nbsp;fname is not None:

#print "match on item %s" % (item)

file2.write("".join(item + "\n"))

#else:

 #print "NOT including item: %s" &nbsp;% (item)

finally:

file2.close()

finally:

file1.close()

&nbsp;

Regards,   
Paul.
