# Global variable error inside tag change script

**URL:** <https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322>\
**Category:** Ignition\
**Created:** [December 19, 2023, 5:20pm UTC](https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322 "2023-12-19T17:20:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [December 19, 2023, 5:20pm UTC](https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322/1 "2023-12-19T17:20:49Z")

</div>

Hello,  
I run this code successfully in the console script but when I try to run this tag change script I get this error: global name 'contents' is not defined  
I define content as global inside my assign function but python doesn't understand it inside the tag change script.

```auto
from StringIO import StringIO
from ftplib import FTP
import system

ftp = FTP()
ftp.connect(ip, port)
ftp.login('','')
ftp.cwd('/LOGS/') 		
contents = ""
def assign(s):
	global contents
	contents += s + "\n"
ftp.retrlines("RETR " + filename, assign)

```

---

<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:** [December 19, 2023, 5:26pm UTC](https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322/2 "2023-12-19T17:26:06Z")

</div>

Move your code to a project library script. Call a function there from a one-liner in the gateway event. You are bumping into some really ancient legacy python scoping. Project library scripts always have sane python scoping. Note, this particular use of "scope" is, unfortunately, unrelated to they use of the word "scope" (Gateway, Vision, Designer, Perspective) anywhere else in Ignition.

---

<div class="post-metadata">

**Author:** ![nader.chinichian](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nader.chinichian/32/9261_2.png) [@nader.chinichian](https://forum.inductiveautomation.com/u/nader.chinichian)\
**Post date:** [December 20, 2023, 6:38am UTC](https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322/3 "2023-12-20T06:38:54Z")

</div>

I still get the error, when I move the code in the project lib and call it in my change tag script:

```auto
def getFTPfileCSV(ip, port, fileName, username = "", password = "", dir = '/LOGS/TICKETSR/'):

	from StringIO import StringIO
	from ftplib import FTP
	import system

	ftp = FTP()
	ftp.connect(ip, port)
	ftp.login(username, password)
	ftp.cwd(dir) 
	#ftp.retrlines('LIST')
	contents = ""

	def assign(s):
		global contents
		contents += s + "\n"
	
	ftp.retrlines("RETR " + fileName, assign)
	ftp.quit()

	return contents

```

---

<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:** [December 20, 2023, 11:47am UTC](https://forum.inductiveautomation.com/t/global-variable-error-inside-tag-change-script/83322/4 "2023-12-20T11:47:40Z")

</div>

I don't see `global contents` in your enclosing function.

Consider not using the `global` keyword at all. Instead, create an object at the top level that you can mutate to hold your persistent content. I usually use dictionaries or dictionary-like objects--you can assign and delete keys in them without needing the `global` keyword.
