Extract a file from Python's module .zip file

35 Views Asked by At

I am developing an Ansible module which needs to work with additional file. When this module is executed, it runs from .zip archive. Does Python have API to read/extract a sibling file from the same archive?

Mine module is executed by Ansible as: /tmp/ansible_mymodule_payload_3p8w168p/ansible_mymodule_payload.zip/ansible/modules/mymodule.py

Where:

  • /tmp/ansible_mymodule_payload_3p8w168p/ansible_mymodule_payload.zip is a zip archive
  • ansible/modules/mymodule.py is relative path inside the .zip archive.

I would like copy file /tmp/ansible_mymodule_payload_3p8w168p/ansible_mymodule_payload.zip/module_utils/xy.py onto filesystem.

I know I could try to split this path manually, but I am curious whether Python has more deterministic API for this.

Like these .zip files are similar to Java .jar files, and in Java there is API which is transparent and can open files even if they are inside .jar archive.

0

There are 0 best solutions below