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