# Os.mkdir not working in some context but in others?

**URL:** <https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [August 5, 2020, 1:30pm UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400 "2020-08-05T13:30:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 5, 2020, 1:30pm UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400/1 "2020-08-05T13:30:09Z")

</div>

I have a gateway message handler script that I use to make folders on a network drive. Depending on the environment determines the base part of the directory path so that I can test on a development system without messing with the real folders. However, while it currently works on my production system, it does not seem to work on my dev system. Here is how I call the gateway message handler -

```auto
def createDirectory(path):
	project = "PSM4_Project"
	messageHandler = "create_edit_Path"
	payload = {}
	payload['mode'] = 1
	payload['pathDictionary'] = path
	result = system.util.sendMessage(project,messageHandler,payload,scope='G')

```

Heres the message handler

```auto
def handleMessage(payload):
	"""
	This message handler will be called each time a message of this
	type is received.

	Arguments:
		payload: A dictionary that holds the objects passed to this
		         message handler. Retrieve them with a subscript, e.g.
		         myObject = payload['argumentName']
	"""
	import os
	import env
	from distutils.dir_util import copy_tree
	logger=system.util.getLogger('Create/Edit')
	logger.info(str(payload))
	path = payload['pathDictionary']
	baseDir = env.getClientFolderBasedOnEnv()
	logger.info("BASE DIR: " + str(baseDir))
    
	if 'entityType' not in payload:
		for key in path.keys():
			finalPath = baseDir + os.path.normpath(path[str(key)])
			logger.info("finalPath: " + str(finalPath))
			if os.path.exists(finalPath):
				logger.info('Path exists')
			else:
				logger.info('Path needs to be created')
				os.makedirs(finalPath)
				logger.info('Folder structure created')
	else:
		for key in path.keys():
			finalPath = baseDir + os.path.normpath(path[str(key)])
			logger.info("finalPath: " + str(finalPath))

			if os.path.exists(finalPath):
				logger.info('Path exists')
			else:
				logger.info('Path needs to be created')
				os.makedirs(finalPath)
				logger.info('Folder structure created')
		if env.isProduction():
			copyFrom = "\\\\192.168.1.226\Bay2\Psi\Compliance\Project Folder Template, Short"
			copyTo = finalPath
			copy_tree(copyFrom, copyTo)
		elif env.isBeta():
			copyFrom = "C:\\Bay1\\Compliance\Project Folder Template, Short"
			copyTo = finalPath
			copy_tree(copyFrom, copyTo)
		elif env.isDev():
			copyFrom = "C:\\Compliance\Project Folder Template, Short"
			copyTo = finalPath
			copy_tree(copyFrom, copyTo)
		else:
			raise ValueError("Cannot determine base folder from environment")

```

And here is the error I get in my gateway logs

```auto
	Exception while attempting to run Python function: Traceback (most recent call last): File "<MessageHandlerScript:PSM4_Project/create_edit_Path >", line 28, in handleMessage File "user-
lib\pylib\os.py", line 167, in makedirs makedirs(head, mode) File "user-lib\pylib\os.py", line 167, in 
makedirs makedirs(head, mode) File "user-lib\pylib\os.py", line 167, in makedirs makedirs(head,
 mode) File "user-lib\pylib\os.py", line 167, in makedirs makedirs(head, mode) File "user
-lib\pylib\os.py", line 167, in makedirs makedirs(head, mode) File "user-lib\pylib\os.py", line 174, in
 makedirs mkdir(name, mode) OSError: [Errno 22] Invalid argument: '\\\\'

```

And here is what determines my base folder -

```auto
def getClientFolderBasedOnEnv():
	"""
	To be used in gateway scripts and components that determine the base folder that folders will be saved on.
	"""
	import env
	if env.isProduction():
		return "\\\\192.168.1.226\\Bay1\\Customer\\"
	elif env.isBeta():
		return "C:\\Bay1\\Customer\\"
	elif env.isDev():
		return "\\\\192.168.1.226\\Bay1\\Customer_DEV_Brian\\"
	else:
		raise ValueError("Cannot determine base folder from environment")

```

Again, this works when `env.isProduction()` is true in my getClientFolderBasedOnEnv function, but not for my `env.isDev()`.

Any insight?

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [February 16, 2023, 9:22am UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400/2 "2023-02-16T09:22:10Z")

</div>

Hi. 🙂  
Have you solved your issue? I'm having a similar problem.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 16, 2023, 1:02pm UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400/3 "2023-02-16T13:02:28Z")

</div>

If you are having an issue with reading/writing to files it's usually either one of two things

1. Scope issue - the script execution is not occurring where you think it is - either in the client but you think it's on the gateway or vice versa

2. Permissions issues - Ignition as a service doesn't have the right to read/write to a certain directory, or the user logged into the client does not.

Seeing the exact error would explain the issue. You should probably make a new post and explain your context.

---

<div class="post-metadata">

**Author:** ![Davide3i](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/davide3i/32/27811_2.png) [@Davide3i](https://forum.inductiveautomation.com/u/Davide3i)\
**Post date:** [February 16, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400/4 "2023-02-16T13:05:45Z")

</div>

I've found [this topic](https://forum.inductiveautomation.com/t/access-a-network-drive/54410/3) and I'm working on it. Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 16, 2023, 1:08pm UTC](https://forum.inductiveautomation.com/t/os-mkdir-not-working-in-some-context-but-in-others/37400/5 "2023-02-16T13:08:27Z")

</div>

Oh yea if you want to have the gateway write to a mapped drive you need to set it up - [Mapping a Network Drive - Ignition User Manual 7.9 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC79/Mapping+a+Network+Drive)
