Python dataitem.export Issues (FGDB Naming)

446
1
Jump to solution
12-21-2021 01:19 PM
babarsac
New Contributor III

I am building a Python script for a client who wants a bunch of hosted feature services exported to file geodatabases. The script I've written works just fine. Each hosted feature service is exported to a GDB then downloaded to a location of my choosing.

The issue is that each unzipped GDB has a seemingly random name. For example:

'Test_Layer_1.zip' once unzipped turns into '_ags_dataD621677BB64F4D2D98CD0D1B1CF805BD.gdb'.

Is there a way to set the name of the GDB as a parameter?

Here is what I've worked out so far...

## Import modules
import os
import datetime
from arcgis.gis import GIS
import zipfile

# Create destination folder 
today = datetime.datetime.now()
path = 'C:/workspace/pythontest' + today.strftime('%m%d%Y')
os.mkdir(path)
dest = path
extension = ".zip"

# Define variables for GIS
PortalUrl = 'https://redacted.redacted.local/portal'
PortalUserName = 'username'
PortalPassword = 'password'
gis = GIS(PortalUrl, PortalUserName, PortalPassword)

# Define input layers
# itemid1 = 'c9325f2f3d7b430b93047230bbe004ac' = 'Test_Layer_1'
# itemid2 = 'edfe40f03dee40839a92ee2bd695cf62' = 'Test_Layer_2'
itemid1 = 'c9325f2f3d7b430b93047230bbe004ac'
itemid2 = 'edfe40f03dee40839a92ee2bd695cf62'

# Export Test_Layer_1
dataitem1 = gis.content.get(itemid1)
dataitem1.export('Test_Layer_1', 'File Geodatabase', parameters=None, wait=True)
myexport1 = gis.content.search('Test_Layer_1', item_type='File Geodatabase')
fgdb1 = gis.content.get(myexport1[0].itemid)
fgdb1.download(save_path=dest, file_name='Test_Layer_1' + ".zip")
fgdb1.delete()

# Export Test_Layer_2
dataitem2 = gis.content.get(itemid2)
dataitem2.export('Test_Layer_2', 'File Geodatabase', parameters=None, wait=True)
myexport2 = gis.content.search('Test_Layer_2', item_type='File Geodatabase')
fgdb2 = gis.content.get(myexport2[0].itemid)
fgdb2.download(save_path=dest, file_name='Test_Layer_2' + ".zip")
fgdb2.delete()

# Unzip ZIP files then delete the ZIP files
os.chdir(dest) # change directory from working dir to dir with files
for item in os.listdir(dest): # loop through items in dir
    if item.endswith(extension): # check for ".zip" extension
        file_name = os.path.abspath(item) # get full path of files
        zip_ref = zipfile.ZipFile(file_name) # create zipfile object
        zip_ref.extractall(dest) # extract file to dir
        zip_ref.close() # close file
        os.remove(file_name) # delete zipped file

 

GIS Discord Channel
https://disboard.org/server/769917190182404127
0 Kudos
1 Solution

Accepted Solutions
AdminGIS2
New Contributor III

I don't think you can rename the gdb using a parameter in the export function. However, you could rename the File Geodatabase after you extracted the zip file using rename, also from os. 

By the way: the export function actually returns the new item so there's no need to search for this after exporting if you catch the new item from the export function. 

View solution in original post

1 Reply
AdminGIS2
New Contributor III

I don't think you can rename the gdb using a parameter in the export function. However, you could rename the File Geodatabase after you extracted the zip file using rename, also from os. 

By the way: the export function actually returns the new item so there's no need to search for this after exporting if you catch the new item from the export function.