如何在Luigi Task中输出zipfile?

时间:2018-11-16 17:15:24

标签: python pipeline luigi

可以像这样output()压缩文件:

def output(self):
    date_path = self.search['date_path']
    zip_fn = "data/%s/%s.zip" % (date_path, date_path)
    return luigi.LocalTarget(zip_fn)

但是如何使用run()方法粘贴此邮政编码?

class ZeroTask(luigi.Task):
    path_in = luigi.Parameter()
    textfiles = []
    path_to_zip = ''

    def requires(self):
        return []

    def run(self):

       # Get bunch of text files
       # some manipulations with textfiles
       # Create a result.zip
       # self.path_to_zip = '~/Project/result.zip'

    def output(self):
        zip_fn = self.path_to_result.zip
        return luigi.LocalTarget(zip_fn)

run()方法做什么?

1 个答案:

答案 0 :(得分:1)

您应该可以根据需要使用zipfile来构建文件。

class MyTask(luigi.Task):
    def output(self):
      date_path = self.search['date_path']
      zip_fn = "data/%s/%s.zip" % (date_path, date_path)
      return luigi.LocalTarget(zip_fn)

     def run(self):
        ztemp = tempfile.NamedTemporaryFile(mode='wb')
        z = zipfile.ZipFile(ztemp, 'w')

        #  build the zip file

        z.close()
        os.rename(ztemp.name, self.output().path)

the docs on FileSystemTarget