Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. 1 #!/usr/bin/env python3.7
  2. 2
  3. 3 import os
  4. 4 import sys
  5. 5
  6. 6 def du(directory, maxdepth=-1):
  7. 7 depth = -1
  8. 8 size = 0
  9. 9
  10. 10 size += os.stat(directory).st_size
  11. 11
  12. 12 def du_rec(directory):
  13. 13 nonlocal depth
  14. 14 nonlocal size
  15. 15
  16. 16 depth += 1
  17. 17
  18. 18 for entry in os.scandir(directory):
  19. 19 if entry.is_file():
  20. 20 size += entry.stat().st_size
  21. 21 if entry.is_dir():
  22. 22 if maxdepth == -1 or depth < maxdepth:
  23. 23 size += entry.stat().st_size
  24. 24 du_rec(entry.path)
  25. 25
  26. 26 du_rec(directory)
  27. 27
  28. 28 return size
  29. 29
  30. 30 if __name__ == '__main__':
  31. 31
  32. 32 directory = sys.argv[1]
  33. 33 maxdepth = -1
  34. 34
  35. 35 if len(sys.argv) > 2:
  36. 36 maxdepth = int(sys.argv[2])
  37. 37
  38. 38 print(du(directory,maxdepth))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement