Select to view content in your preferred language

Managing Temp files when using arcpy.mp.ArcGISProject

287
1
05-24-2024 12:40 AM
JasonBatory
New Contributor II

I have a Python script that uses ArcPy to open an ArcGIS Pro Project and export pages of a Map Series to PDF files.

The Map used by the Map Series contains 2 WMS layers and some other SDE layers.

It's running successfully but I am trying to manage the temporary files that are being generated. As the Map Series can be hundreds of pages and the WMS services generate many large png files, I have found that the Temp directory ArcGIS Pro uses is filling the disk to 100%.

When ArcGIS Pro is opened via desktop, the Temp directory gets created by default in C:\Users\<username>\AppData\Local\Temp\ArcGISProTemp<random number>.
When ArcGIS Pro is closed this directory is deleted.

When I run my script, the Temp directory used is just: C:\Users\<username>\AppData\Local\Temp (no extra folder is created) and any files generated by the script do not get deleted when the script ends.

I tried changing the location of the Temp directory to a disk with more space by setting the environment variable ARCTMPDIR. This worked using ArcGIS Pro desktop but when I ran my code it still
used C:\Users\<username>\AppData\Local\Temp.

So, is it possible to set the location of this Temp directory using Python, and therefore clean it up as I go? Ideally after each page of the Map Series has finished exporting.

Tags (3)
0 Kudos
1 Reply
HaydenWelch
Occasional Contributor II

Python has a builtin tempfile module that you can use to make a tempfile then use shutil.rmtree when you're done:

 

import shutil
import tempfile
import os

def mk_tempdir(location: os.PathLike = None):
    fd, temp_file_name = tempfile.mkdtemp(dir=location)
    return temp_file_name

def del_tempdir(temp_file_name: os.PathLike):
    shutil.rmtree(temp_file_name)
    return

 

 

0 Kudos