0

I need to download a zipped archive from a URL and than modify it using python, I was able to do the downloading and extraction part but I came accross a little problem, the archive is uploaded monthly and its name is changed everytime, so I need to be able to change its name so I can later work with the Excel file it gives me

that is my code so far

import requests, zipfile, io

url = "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php"

save_path = "C:/Users/gb2gaet"
print("user")
u = input()
print("password")
p= input()
tp = "http://" + u + ":" + p + "@10.193.33.228:8080"
proxies = {
"http": tp,
"https": tp,
}
r = requests.get(url, proxies=proxies, stream=True, verify=False )
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall(save_path)

I tryied using this thread but it didn't work well for me since the approaches are a little bit different, or it might be that I just didn't do it right, idk

  • 1
    What about [this](https://stackoverflow.com/questions/30957064/renaming-zipfile-in-python#30957137) thread? – OctaveL Nov 27 '20 at 16:40
  • @OctaveL well, this one only shows that this is not possible, since I am downloading it I can't extract and rename it, anyways, thanks for answering at least now I know that I should start looking for another possible solution –  Nov 27 '20 at 17:02
  • I don't follow, what's preventing you from extracting it? – OctaveL Nov 27 '20 at 17:13
  • @OctaveL i can't extact AND change its name, that is my problem, because after extratcing I need to work with the file and since the name changes everytime I can't tell python to open "this" file since I don't know its name. I need to find a way to tell my program which file it is knowing only in which folder it is and that it is the only file in that folder –  Nov 27 '20 at 17:17
  • Why not create a new zipfile you'll name yourself and write the downloaded bytes to it? – OctaveL Nov 27 '20 at 17:22
  • I'd rather use `curl -sSL "https://servicos.ibama.gov.br/ctf/publico/areasembargadas/downloadListaAreasEmbargadas.php" -o new.zip && unzip new.zip`. :P – KaiserKatze Nov 28 '20 at 05:20
  • Does this answer your question? [Renaming ZipFile in Python](https://stackoverflow.com/questions/30957064/renaming-zipfile-in-python) – KaiserKatze Nov 28 '20 at 05:23

0 Answers0