filetools.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import os
  2. import shutil
  3. script_path = os.path.dirname(os.path.realpath(__file__))
  4. class FileTools:
  5. def mkdir(self, dir):
  6. real = self.get_ab_path(dir)
  7. if not os.path.exists(real):
  8. try:
  9. os.mkdir(real)
  10. return True
  11. except:
  12. return False
  13. def write(self, file, content):
  14. try:
  15. with open(self.get_ab_path(file), 'w') as f:
  16. f.write(content)
  17. return True
  18. except:
  19. return False
  20. def write_b(self, file, content):
  21. try:
  22. print(file)
  23. with open(self.get_ab_path(file), 'wb') as f:
  24. f.write(content)
  25. return True
  26. except Exception as e:
  27. print(e)
  28. return False
  29. def rm(self, dir):
  30. full=self.get_ab_path(dir)
  31. try:
  32. shutil.rmtree(path=full)
  33. return True
  34. except:
  35. return False
  36. def get_ab_path(self,path):
  37. return "%s/%s" % (script_path, path)