# Gateway Script to monitor directory for new files

**URL:** <https://forum.inductiveautomation.com/t/gateway-script-to-monitor-directory-for-new-files/16124>\
**Category:** Ignition\
**Created:** [November 26, 2017, 11:02pm UTC](https://forum.inductiveautomation.com/t/gateway-script-to-monitor-directory-for-new-files/16124 "2017-11-26T23:02:32Z")\
**Posts on this page:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![bill.diblasi](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@bill.diblasi](https://forum.inductiveautomation.com/u/bill.diblasi)\
**Post date:** [June 11, 2018, 5:12pm UTC](https://forum.inductiveautomation.com/t/gateway-script-to-monitor-directory-for-new-files/16124/22 "2018-06-11T17:12:42Z")

</div>

I wanted to update with a solution for an obstacle I ran into when trying to open files identified by this watcher. I saw the following regardless of using `WatchService.take()` or `WatchService.poll()`.

- File paths are reliably collected and always immediately verifiable with `os.path(path)` returning `True`
- Upon immediate inspection after a WatchService notification, some files return `False` from `Files.isReadable(path)` and will throw `IOError[2] No such file` if opened
- After a short delay (i.e. 3 seconds) these files can be opened

I used this check to ensure every new file is eventually opened:

```auto
import os
from java.nio.file import Files, Paths
from time import sleep

pathCheck = os.path.isfile(path)
readCheck = Files.isReadable(Paths.get(path))
target = None

if pathCheck & readCheck:
	target = open(path, 'rb')
else:
	for i in range(5):
		sleep(3)
		try:
			target = open(path, 'rb')
			break
		except IOError, e:
			if e.errno == 2:
				pass
			else:
				raise

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/gateway-script-to-monitor-directory-for-new-files/16124)._
