# How to check that a file is open

**URL:** <https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618>\
**Category:** Ignition\
**Created:** [January 27, 2017, 8:11pm UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618 "2017-01-27T20:11:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmylooo](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@jimmylooo](https://forum.inductiveautomation.com/u/jimmylooo)\
**Post date:** [January 27, 2017, 8:11pm UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618/1 "2017-01-27T20:11:33Z")

</div>

I’ve been trying to find the best way to check to see if a file is open. This is what I’ve come up with so far:

```auto
import os
filepath = "\\\\c:\\TST\\MyFile.txt"
isOpen = None
file_object = None
if os.path.exists(filepath):
	try:
		print "Trying to open %s." % filepath
		buffer_size = 8
		# Opening file in append mode and read the first 8 characters.
		file_object = open(filepath, 'a', buffer_size)
		if file_object:
			print "%s is not open." % filepath
			isOpen = False
	except IOError, message:
		print "File is open (unable to open in append mode). %s." % \
			  message
		isOpen = True
	finally:
		if file_object:
			file_object.close()
			print "%s closed." % filepath
else:
	print "%s not found." % filepath
print isOpen
```

The code above works great when it is used on a button, but when it is implemented in the shared scripts file like this:

```auto
import os

def isOpen(filepath):
	
	isOpen = None
	file_object = None
	if os.path.exists(filepath):
		try:
			print "Trying to open %s." % filepath
			buffer_size = 8
			# Opening file in append mode and read the first 8 characters.
			file_object = open(filepath, 'a', buffer_size)
			if file_object:
				print "%s is not open." % filepath
				isOpen = False
		except IOError, message:
			print "File is open (unable to open in append mode). %s." % \
				  message
			isOpen = True
		finally:
			if file_object:
				file_object.close()
				print "%s closed." % filepath
	else:
		print "%s not found." % filepath
	return isOpen
```

The following error occurs:

Traceback (most recent call last):  
File “event:actionPerformed”, line 26, in   
File “module:shared.file”, line 13, in isOpen  
File “module:shared.file”, line 13, in isOpen  
TypeError: open() takes exactly 1 argument (3 given)

We are using:  
Ignition 7.9.0 (b2016101208)  
Windows Server 2012R2  
Java 1.8.0\_111-b14

Any help would be great!

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [January 30, 2017, 7:39pm UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618/2 "2017-01-30T19:39:04Z")

</div>

Seems to work for me (in the sense that both scripts do the same thing). How are you calling the shared script?

I assume you’re trying to access a file being written to by another application. Unfortunately this is really hard to do without running into file locking and race conditions.

---

<div class="post-metadata">

**Author:** ![jimmylooo](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@jimmylooo](https://forum.inductiveautomation.com/u/jimmylooo)\
**Post date:** [February 3, 2017, 6:17pm UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618/3 "2017-02-03T18:17:22Z")

</div>

Yes there are nuances I agree. But my question is: Why does the same code work differently when called from the Project/Shared Scripts definition vs the code being placed on a simple button action performed event?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 3, 2017, 7:17pm UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618/4 "2017-02-03T19:17:23Z")

</div>

What you’ve described would be possible if you’ve defined a function named ‘open’ in that module. That would ‘hide’ the built-in open() function from everything else in the module.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 4, 2017, 12:05am UTC](https://forum.inductiveautomation.com/t/how-to-check-that-a-file-is-open/12618/5 "2017-02-04T00:05:08Z")

</div>

To add to Phil - I think what’s might be happening is the Jython interpreter thinking ‘isOpen’ is equivalent to an open() call.
