QCTG

GZIP downloaden und entpacken

Mar 25th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 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. # Verbindung mit FTP-Server aufbauen
  28. ftp = FTP_TLS(ftp_host)
  29. ftp.auth()
  30. ftp.login(user=ftp_user, passwd=ftp_password)
  31. ftp.prot_p()
  32.  
  33. # Liste der herunterzuladenen Dateien durchlaufen
  34. file_list = pd.read_csv(file_list_name, sep=";")
  35. for i, row in file_list.iterrows():
  36. filename_zip = ".xml.gz"
  37. filename_xml_src = "%s.xml" % row["Dateiname"]
  38. filename_xml_dest = "%s.xml" % (row["Dateiname"])
  39. filepath_remote = filepath_root_remote + "/%s%s" % (row["Dateiname"], filename_zip)
  40. filepath_local = "%s/%s" % (filepath_root_local, row["Dateiname"]) + filename_zip
  41. # Zip Datei herunterladen
  42. with open(filepath_local, "wb") as file_local:
  43. ftp.retrbinary("RETR %s" % filepath_remote, file_local.write)
  44. # XML Datei entpacken
  45. zip_ref = ZipFile(filepath_local, "r")
  46. zip_ref.extract(filename_xml_src, path=filepath_root_local)
  47. zip_ref.close()
  48. # FTP Verbindung schliessen
  49. ftp.quit()
Add Comment
Please, Sign In to add comment