Select to view content in your preferred language

Intermediate data in stand-alone Python scripts

1257
7
Jump to solution
01-25-2023 06:15 AM
NSamu
by
Regular Contributor

Hello,

Is it possible to write to memory in stand-alone Python scripts using \memory? I haven't been able to get it to work but I thought that this was possible. If not, what is a good practice for dealing with intermediate data in stand-alone scripts?

Thanks,

Nicole

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

You can.  And it takes some trial and error for the placement of the slash since sometimes just memory works, and other times, you need to add it to the beginning.  There is a question on here posted not too long ago that demonstrates how arcpy.Exists() will either fail or resolve depending on the leadning /.  There isn't any mention in the docs about the leading /, but the interpreter doesn't lie.

existence-of-data-in-memory-and-in-memory 

 

View solution in original post

7 Replies
Taren_Esri
Esri Contributor

Hi @NSamu 

It looks like your / is out of place. Try having your path beginning with memory/ and take a look at our documentation on this topic here. 

 

Best,

Taren

 

NSamu
by
Regular Contributor

Thank you. I read the documentation but wasn't clear about whether or not you could use /memory in standalone scripts but I do use it quite a bit inside of Pro. Glad to know that this is possible.

0 Kudos
by Anonymous User
Not applicable

You can.  And it takes some trial and error for the placement of the slash since sometimes just memory works, and other times, you need to add it to the beginning.  There is a question on here posted not too long ago that demonstrates how arcpy.Exists() will either fail or resolve depending on the leadning /.  There isn't any mention in the docs about the leading /, but the interpreter doesn't lie.

existence-of-data-in-memory-and-in-memory 

 

NSamu
by
Regular Contributor

Thank you @Anonymous User. I appreciate the clarification. I will trial/error with and without the leading slash in stand-alone scripts.

0 Kudos
DannyMcVey
Esri Alum

Hi Nicole,

What error(s) are you seeing and what version of ArcGIS are you using? That should work for geoprocessing tools in stand-alone scripts.

A non-arcpy (gasp!) alternative is to use the python tempfile library to create a temporary file or directory, and use it to store intermediate data:

import tempfile

temp_file = tempfile.NamedTemporaryFile()
print(temp_file.name) # prints the path of the created temporary file

# ... do something with the temporary file ...

temp_file.close() # deletes the file

 This is stored on disk, so the performance benefits don't apply, but this is a similarly clean way to keep your scripts from needlessly cluttering up your workspace. I've seen this used very effectively. 

NSamu
by
Regular Contributor

@DannyMcVey , thank you for this suggestion. I appreciate it. I may end up doing that since writing to memory is still being a finicky from the stand-alone script. Here's the error (I took out part of the file paths for privacy reasons).

Traceback (most recent call last):
File "P:/GISData/.../Create_IDBData.py", line 8, in <module>
arcpy.management.PolygonToLine(r"P:\DatabaseConnection\....DBO.TNCountyBoundary", r"\memory\county_line_temp", "IGNORE_NEIGHBORS")
File "C:\Users\nsamu\AppData\Local\Programs\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4664, in PolygonToLine
raise e
File "C:\Users\...ArcPy\arcpy\management.py", line 4661, in PolygonToLine
retval = convertArcObjectToPythonObject(gp.PolygonToLine_management(*gp_fixargs((in_features, out_feature_class, neighbor_option), True)))
File "C:\Users\...\AppData\Local\Programs\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000210: Cannot create output \memory\county_line_temp
Failed to execute (PolygonToLine).