cstrap
3/9/2016 - 9:11 AM

Sample of "null writing" to file.

Sample of "null writing" to file.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import io

class File(io.FileIO):

    def __init__(self, name, mode='r', closefd=True, **kwargs):
        self.wf = kwargs.pop('wf', True)
        super(File, self).__init__(name, mode, closefd)

    def write(self, line,*args, **kwargs):
        if self.wf:
            super(File, self).write(line)


if __name__ == '__main__':
  """
  foo.txt has some rows
  if `wf=True` class makes a copy of `foo.txt` in `bar.txt` 
  """
  with File('foo.txt', 'r') as foo:
      with File('bar.txt', 'w', wf=True) as bar:
          for line in foo:
              bar.write(line)
              
  """
  foo.txt has some rows
  if `wf=False` make a touch of `baz.txt` and the class doesn't write lines 
  """
  with File('foo.txt', 'r') as foo:
      with File('baz.txt', 'w', wf=False) as bar:
          for line in foo:
              bar.write(line)