@@ -350,7 +350,8 @@ def __init__(self,
350350 shard_name_template = None ,
351351 coder = coders .ToBytesCoder (), # type: coders.Coder
352352 compression_type = CompressionTypes .AUTO ,
353- header = None ):
353+ header = None ,
354+ footer = None ):
354355 """Initialize a _TextSink.
355356
356357 Args:
@@ -382,6 +383,8 @@ def __init__(self,
382383 compression.
383384 header: String to write at beginning of file as a header. If not None and
384385 append_trailing_newlines is set, '\n ' will be added.
386+ footer: String to write at the end of file as a footer. If not None and
387+ append_trailing_newlines is set, '\n ' will be added.
385388
386389 Returns:
387390 A _TextSink object usable for writing.
@@ -396,6 +399,7 @@ def __init__(self,
396399 compression_type = compression_type )
397400 self ._append_trailing_newlines = append_trailing_newlines
398401 self ._header = header
402+ self ._footer = footer
399403
400404 def open (self , temp_path ):
401405 file_handle = super (_TextSink , self ).open (temp_path )
@@ -405,6 +409,13 @@ def open(self, temp_path):
405409 file_handle .write (b'\n ' )
406410 return file_handle
407411
412+ def close (self , file_handle ):
413+ if self ._footer is not None :
414+ file_handle .write (coders .ToBytesCoder ().encode (self ._footer ))
415+ if self ._append_trailing_newlines :
416+ file_handle .write (b'\n ' )
417+ super (_TextSink , self ).close (file_handle )
418+
408419 def display_data (self ):
409420 dd_parent = super (_TextSink , self ).display_data ()
410421 dd_parent ['append_newline' ] = DisplayDataItem (
@@ -588,7 +599,8 @@ def __init__(
588599 shard_name_template = None , # type: Optional[str]
589600 coder = coders .ToBytesCoder (), # type: coders.Coder
590601 compression_type = CompressionTypes .AUTO ,
591- header = None ):
602+ header = None ,
603+ footer = None ):
592604 r"""Initialize a :class:`WriteToText` transform.
593605
594606 Args:
@@ -624,6 +636,9 @@ def __init__(
624636 header (str): String to write at beginning of file as a header.
625637 If not :data:`None` and **append_trailing_newlines** is set, ``\n`` will
626638 be added.
639+ footer (str): String to write at the end of file as a footer.
640+ If not :data:`None` and **append_trailing_newlines** is set, ``\n`` will
641+ be added.
627642 """
628643
629644 self ._sink = _TextSink (
@@ -634,7 +649,8 @@ def __init__(
634649 shard_name_template ,
635650 coder ,
636651 compression_type ,
637- header )
652+ header ,
653+ footer )
638654
639655 def expand (self , pcoll ):
640656 return pcoll | Write (self ._sink )
0 commit comments