Source code for pyquickhelper.filehelper.transfer_api_file

API to move files using FTP

import os
from ..loghelper import noLOG
from .transfer_api import TransferAPI

[docs]class TransferAPIFile(TransferAPI): """ defines an API to transfer files over another location :githublink:`%|py|13` """
[docs] def __init__(self, location, fLOG=noLOG): """ constructor :param location: location :param fLOG: logging function :githublink:`%|py|21` """ TransferAPI.__init__(self, fLOG=fLOG) self._location = location
[docs] def transfer(self, path, data): """ It assumes a data holds in memory, tansfer data to path. :param data: bytes :param path: path to remove location :return: boolean :githublink:`%|py|33` """ src = os.path.join(self._location, path) fol = os.path.dirname(src) if not os.path.exists(fol): os.makedirs(fol) with open(src, "wb") as f: f.write(data) return True
[docs] def retrieve(self, path, exc=True): """ retrieve data from path :param path: remove location :param exc: keep exception :return: data :githublink:`%|py|49` """ src = os.path.join(self._location, path) if os.path.exists(src): with open(src, "rb") as f: return elif exc: raise FileNotFoundError(path) else: return None