Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. # -*- coding: UTF-8 -*-
  2. __author__ = 'Chernov.Artem'
  3.  
  4. import os
  5.  
  6. class FileHelper:
  7.  
  8. @staticmethod
  9. def read(path):
  10. """
  11. Function read data from file
  12.  
  13. :param pathToFile: absolut path to file
  14. :return:
  15. """
  16. if not os.path.isfile(path):
  17. raise Exception('File {0} not found'.format(path))
  18. with open(path, 'r', encoding='utf-8') as f:
  19. toReturn = [line.strip() for line in f if line.strip() != '']
  20.  
  21. if not len(toReturn):
  22. raise Exception('File "' + path + '" empty')
  23. return toReturn
  24.  
  25. @staticmethod
  26. def write(path, data, mode='w'):
  27. """
  28. Function for write data to file
  29.  
  30. :param path: absolut path to file
  31. :param data: data which need saving to file
  32. :param mode: mode for file
  33. :return:
  34. """
  35. if type(data) == list:
  36. data = "\n".join(data)
  37.  
  38. with open(path, mode, encoding="utf-8") as f:
  39. f.write(data + '\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement