Select to view content in your preferred language

Can someone help me fix this Automated Download script?

1125
2
Jump to solution
07-20-2020 03:17 PM
DinubaAGOLAdmin
Emerging Contributor

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. 

Traceback (most recent call last):
  File "C:/Users/brossi/Desktop/download_data.py", line 34, in <module>
    zip_file = ZipFile(zip_path)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\zipfile.py", line 1113, in __init__
    self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: 'G:\\Dinuba\x820\\ArcGISOnline\\Fire_hydrant_CSVs\\hydrant20200720'
>>>
I disabled the timestamp because I just wanted to get the script working but for some reason no matter what I do it keeps coming up with  this. 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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))

View solution in original post

0 Kudos
2 Replies
BenTurrell
Frequent Contributor

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.

JakeSkinner
Esri Esteemed Contributor

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))
0 Kudos