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

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 -

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

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

	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 -

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?

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

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.

1 Like

I've found this topic and I'm working on it. Thank you. :slight_smile:

1 Like

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

1 Like