Python - Download Zip File

1609
4
Jump to solution
09-01-2021 06:01 AM
AlexP_
by
Occasional Contributor III

Hello,

I am using python notebook on ArcGIS Pro 2.8. Please see script and screenshots. I was able to get data zip file download but unable to extract data zip file. It has FGBD as another zip file inside the zip file folder. Am I missing something?? Please kindly assist as soon as possible. I am not sure which group I can post. Thank you, Alex

 

from arcgis.gis import GIS

# Python Standard Library Modules
from pathlib import Path, PurePath
from zipfile import ZipFile

public_data_item_id = '123'

anon_gis = GIS(username="123", password="123")

data_item = anon_gis.content.get(public_data_item_id)

data_path = Path('C:\123')

if not data_path.exists():
    data_path.mkdir()

zip_path = data_path.joinpath('PAData.zip')
extract_path = data_path.joinpath('PADataDatasets')

data_item.download(save_path=data_path)

print (PurePath(zip_path))

 

AlexP__0-1630500985280.pngAlexP__1-1630500996117.png

 

0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

Try this solution

with ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(extract_path)

 

View solution in original post

4 Replies
BlakeTerhune
MVP Regular Contributor

Try this solution

with ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(extract_path)

 

AlexP_
by
Occasional Contributor III

Hello @BlakeTerhune   Please see screenshots and python script. i also set it up on task scheduler. it was working running since September 2021 until stop recently. I haven't change any script and task scheduler.  For some reasons, my zip file 'PaFiles.gdb' stopped working. I found out that the last time inside that zip file, it ran populated on Jan. 2022 but everything else is fine.  Please advise. Thank you for your help.

AlexP__1-1647960880536.png

AlexP__2-1647961091882.png

 

 

AlexP__0-1647959878029.png

 

 

from arcgis.gis import GIS

# Python Standard Library Modules
from pathlib import Path, PurePath
from zipfile import ZipFile

public_data_item_id = '123'

anon_gis = GIS(username="123", password="123")

data_item = anon_gis.content.get(public_data_item_id)

data_path = Path('C:\123')

if not data_path.exists():
    data_path.mkdir()
 
    
zip_path = data_path.joinpath('PAData.zip')
extract_path = data_path.joinpath('PADataDatasets')

with ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(extract_path)  
    

data_item.download(save_path=data_path)

print (PurePath(zip_path))

from pathlib import Path, PurePath
from zipfile import ZipFile

data_path = Path('C:\123\PADataDatasets\PA_Data')

if not data_path.exists():
    data_path.mkdir()
 
    
zip_path = data_path.joinpath('PaFiles.gdb.zip')
extract_path = data_path.joinpath('PaFiles.gdb')

with ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(extract_path)  
    

print (PurePath(zip_path))

 

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

If it was working fine, it must be an access issue. I would start by logging in to the computer running the scheduled task as the user the scheduled task is running as, then see if you can run the script inside of an IDE there. If that works, then there's a configuration issue with Windows Task Scheduler. If it doesn't work, you should be able to debug it from the IDE.

0 Kudos
AlexP_
by
Occasional Contributor III

I ran it manually from task scheduler, and it is working. It seems like configuration issue with windows task. I am not sure where to narrow it down. It hasn't been changed. 

0 Kudos