File Read From Gateway Directory

Hello,

I have a gateway Timers to look for the new file in a mapped drive on gateway !!

Here’s how the script looks like -

import time
import threading
import os	
import glob

def getFile():
	machine = "MDR 1"
	machineFolder = "MDR1"
	directory = glob.iglob('E:\\' + machineFolder + '\\*.asc')
	newFile = max(directory, key =os.path.getctime)
	
	previousFile = str(system.tag.read("File Directory/%s" % (machine)).value)


	if str(previousFile) != str(newFile):
		file = open(newFile, "r")
		data = [line for line in file.readlines()]
		file.close()
		system.tag.write("File Directory/%s" % (machine), newFile )
		return data, machine
	else:
		print 'File is processed !'
		return None, None

data, machine = getFile()

if data :
	project.LabTesting.parseAlphaFile(data, machine)

But I am getting the following error on gateway -

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 25, in File "", line 9, in getFile NameError: global name 'glob' is not defined

at org.python.core.Py.NameError(Py.java:260)

at org.python.core.PyFrame.getglobal(PyFrame.java:265)

at org.python.pycode._pyx40016.getFile$1(:23)

at org.python.pycode._pyx40016.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyBaseCode.call(PyBaseCode.java:120)

at org.python.core.PyFunction.__call__(PyFunction.java:307)

at org.python.pycode._pyx40016.f$0(:28)

at org.python.pycode._pyx40016.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:165)

at org.python.core.PyCode.call(PyCode.java:18)

at org.python.core.Py.runCode(Py.java:1275)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:636)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:603)

at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:88)

at java.util.TimerThread.mainLoop(Unknown Source)

at java.util.TimerThread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 25, in File "", line 9, in getFile NameError: global name 'glob' is not defined

... 17 common frames omitted

I thought, Including the Glob library would be a easy fix but doesn’t seem to work.

Thanks

Gateway event scripts have legacy scope rules. Either move everything to a script module and call the function from there, or import glob within your nested function definition.

Okay ! That works, Thanks