from arcgis.gis import GIS
#from datetime import datetime
#Python Standard Library modules
from pathlib import Path
from zipfile import ZipFile
print('import complete')
#store the IF of the public data item being downloaded
data_item_id = 'a3e1fb26f38d487480299b570994ffce'
#login to ArcGIS Online
#user =
#password = '
gis = GIS(username='x, password='!x')
print('Login successful')
#use API to retrieve and item object data_item
data_item = gis.content.get(data_item_id)
#if no Item with that Id, return None
data_item
#download zipfile to the server's current location r'G:\Dinuba\2020\ArcGISOnline\Fire_hydrant_CSVs'
data_path = Path(r'G:\Dinuba\2020\ArcGISOnline\Fire_hydrant_CSVs')
#timestamp = time.strftime('%Y%m%d')
zip_path = data_path.joinpath('hydrant.zip')
extract_path = data_path.joinpath('FireHydrants')
data_item.download(save_path=data_path)
#extract zipfile, make list of contents in directory
zip_file = ZipFile(zip_path)
zip_file.extractall(path=extract_path)
list(file.name for file in extract_path.glob('*'))
print('Download Complete')
This is the error I keep getting. I don't understand what I am doing wrong.
Solved! Go to Solution.
DinubaAGOL Admin I've used the following to download and extract a File Geodatabase from AGOL:
from arcgis.gis import GIS
from zipfile import ZipFile
# Variables
portal = 'https://www.arcgis.com'
username = 'jskinner_CountySandbox'
password = '*****'
itemID = '492e91060dcd4080be22b4d967655004'
exportFolder = r"C:\temp"
# Connect to portal
gis = GIS(portal, username, password)
# Get item
item = gis.content.get(itemID)
# Export hosted feature service to FGD, and downlaod
export_name = "export_" + item.title
result_item = item.export(export_name, 'File Geodatabase', wait=True)
print("Exported: {}".format(result_item))
download_result = result_item.download(exportFolder)
print("Saved to: {}".format(download_result))
result_item.delete()
print("Deleted result")
# Extract zip file
with ZipFile(download_result, 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extractall(exportFolder)
print("Unzipped {0}".format(download_result))
Hey DinubaAGOL Admin,
I'd personally do a print on the zip_path variable after you create it. It looks as though the path is misinterpreting the backslashes and as such it can't find the folder path. You can see in your error the path has only one backslash around the 2020 part and has changed it: G:\\Dinuba\x820\\ArcGISOnline\\Fire_hydrant_CSVs\\hydrant20200720
You can further test this by hardcoding the path here:
zip_file = ZipFile(zip_path)
to
zip_file = ZipFile(r'G:\Dinuba\2020\ArcGISOnline\Fire_hydrant_CSVs\firehydrant.zip')
IF this is where the error is you could try using os.path.join. I haven't personally used pathlib so I'm not sure of its capabilities or limitations: Python | os.path.join() method - GeeksforGeeks
Happy to discuss more.
Thanks
Ben
If this answer was helpful please mark it as helpful. If this answer solved your question please mark it as the answer to help others who have the same question.
DinubaAGOL Admin I've used the following to download and extract a File Geodatabase from AGOL:
from arcgis.gis import GIS
from zipfile import ZipFile
# Variables
portal = 'https://www.arcgis.com'
username = 'jskinner_CountySandbox'
password = '*****'
itemID = '492e91060dcd4080be22b4d967655004'
exportFolder = r"C:\temp"
# Connect to portal
gis = GIS(portal, username, password)
# Get item
item = gis.content.get(itemID)
# Export hosted feature service to FGD, and downlaod
export_name = "export_" + item.title
result_item = item.export(export_name, 'File Geodatabase', wait=True)
print("Exported: {}".format(result_item))
download_result = result_item.download(exportFolder)
print("Saved to: {}".format(download_result))
result_item.delete()
print("Deleted result")
# Extract zip file
with ZipFile(download_result, 'r') as zipObj:
# Extract all the contents of zip file in current directory
zipObj.extractall(exportFolder)
print("Unzipped {0}".format(download_result))