Guest User

XML GZIP downloaden und entpacken

a guest
Mar 25th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import os
  2. import gzip
  3. from glob import glob
  4. from ftplib import FTP_TLS
  5. from zipfile import ZipFile
  6. import xml.etree.ElementTree as ElementTree
  7. import html
  8. import pandas as pd
  9. import numpy as np
  10. from slugify import slugify
  11.  
  12. # Konfiguration
  13. ftp_host = "ip"
  14. ftp_user = "user"
  15. ftp_password = "pass"
  16.  
  17. filepath_root_remote = "/public_html/export"
  18. filepath_root_local = "P:/Pfad/Onlineshop/Sitemap"
  19. file_list_name = "P:/Pfad/Onlineshop/Sitemap/Python-Dateiimport.csv"
  20.  
  21. # Verbindung mit FTP-Server aufbauen
  22. ftp = FTP_TLS(ftp_host)
  23. ftp.auth()
  24. ftp.login(user=ftp_user, passwd=ftp_password)
  25. ftp.prot_p()
  26.  
  27. # Liste der herunterzuladenen Dateien durchlaufen
  28. file_list = pd.read_csv(file_list_name, sep=";")
  29. for i, row in file_list.iterrows():
  30. filename_zip = "%s_xml.zip" % row["Modellid"]
  31. filename_xml_src = "%s.xml" % row["Modellid"]
  32. filename_xml_dest = "%s_%s.xml" % (row["Herstellerid"], row["Modellid"])
  33. filepath_remote = filepath_root_remote + "/%s/XML/%s" % (row["Herstellerid"], filename_zip)
  34. filepath_local = "%s/%s_%s" % (filepath_root_local, row["Herstellerid"], filename_zip)
  35. # Zip Datei herunterladen
  36. with open(filepath_local, "wb") as file_local:
  37. ftp.retrbinary("RETR %s" % filepath_remote, file_local.write)
  38. # XML Datei entpacken
  39. zip_ref = ZipFile(filepath_local, "r")
  40. zip_ref.extract(filename_xml_src, path=filepath_root_local)
  41. zip_ref.close()
  42. # XML Datei umbenennen
  43. os.rename(os.path.join(filepath_root_local, filename_xml_src),
  44. os.path.join(filepath_root_local, filename_xml_dest))
  45. # Zip Datei löschen
  46. os.remove(filepath_local)
  47.  
  48. # FTP Verbindung schliessen
  49. ftp.quit()
Add Comment
Please, Sign In to add comment