Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. def printStructure(struct, stream = stdout):
  2. for e in struct.element:
  3. print >> stream, struct
  4.  
  5. #! /usr/bin/env python
  6. # -*- coding: utf-8 -*-
  7. import sys
  8.  
  9.  
  10. def printStructure(struct, stream=sys.stdout):
  11. if stream != sys.stdout:
  12. sys.stdout = stream
  13. print struct
  14. else:
  15. print struct
  16.  
  17. if __name__ == "__main__":
  18. printStructure("Print to file: 0", open('out.log', 'w'))
  19. printStructure("Print to console: 1")
  20. printStructure("Print to file: 1", open('out.log', 'aw'))
  21. printStructure("Print to file: 2", open('out.log', 'aw'))
  22. printStructure("Print to console: 2")
  23.  
  24. stream = open("myfile.txt", "w")
  25. print("Some text", out=stream) # prints "Some text" to myfile.txt
  26.  
  27. print("Some text") # prints "Some text" to stdout
  28.  
  29. from __future__ import print_function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement