dir_size.py
来源:互联网 发布:mac ppt设置不能修改 编辑:程序博客网 时间:2024/06/10 00:18
# -*- encoding: utf-8 -*-
import os
from os.path import *
class DirSizeError(Exception): pass
def dir_size(start, follow_links=0, start_depth=0, max_depth=0, skip_errs=0):
# Get a list of all names of files and subdirectories in directory start
try: dir_list = os.listdir(start)
except:
# If start is a directory, we probably have permission problems
if os.path.isdir(start): #译注:没有读权限
raise DirSizeError('Cannot list directory %s'%start)
else: # otherwise, just re-raise the error so that it propagates
raise
total = 0L
for item in dir_list:
# Get statistics on each item--file and subdirectory--of start
path = join(start, item)
try: stats = os.stat(path)
except:
if not skip_errs:
raise DirSizeError('Cannot stat %s'%path) #译注:没有读权限
# The size in bytes is in the seventh item of the stats tuple, so:
total += stats[6]
# recursive descent if warranted
if isdir(path) and (follow_links or not islink(path)): #译注:遍历计算子目录
bytes = dir_size(path, follow_links, start_depth+1, max_depth)
total += bytes
if max_depth and (start_depth < max_depth):
print_path(path, bytes, units)
return total
def print_path(path, bytes, units='b'):
if units == 'k':
print '%-8ld%s' % (bytes / 1024, path)
elif units == 'm':
print '%-5ld%s' % (bytes / 1024 / 1024, path)
else:
print '%-11ld%s' % (bytes, path)
def usage (name):
print "usage: %s [-bkLm] [-d depth] directory [directory...]" % name
print '/t-b/t/tDisplay in Bytes (default)'
print '/t-k/t/tDisplay in Kilobytes'
print '/t-m/t/tDisplay in Megabytes'
print '/t-L/t/tFollow symbolic links (meaningful on Unix only)'
print '/t-d, --depth/t# of directories down to print (default = 0)'
if __name__=='__main__':
# When used as a script:
import string, sys, getopt
units = 'b'
follow_links = 0
depth = 0
try:
opts, args = getopt.getopt(sys.argv[1:], "bkLmd:", ["depth="]) #译注:解析命令行参数
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(1)
for o, a in opts:
if o == '-b': units = 'b'
elif o == '-k': units = 'k'
elif o == '-L': follow_links = 1
elif o == '-m': units = 'm'
elif o in ('-d', '--depth'):
try: depth = int(a)
except:
print "Not a valid integer: (%s)" % a
usage(sys.argv[0])
sys.exit(1)
if len(args) < 1:
print "No directories specified"
usage(sys.argv[0])
sys.exit(1)
else:
paths = args
for path in paths:
try: bytes = dir_size(path, follow_links, 0, depth)
except DirSizeError, x: print "Error:", x
else: print_path(path, bytes,units)
import os
from os.path import *
class DirSizeError(Exception): pass
def dir_size(start, follow_links=0, start_depth=0, max_depth=0, skip_errs=0):
# Get a list of all names of files and subdirectories in directory start
try: dir_list = os.listdir(start)
except:
# If start is a directory, we probably have permission problems
if os.path.isdir(start): #译注:没有读权限
raise DirSizeError('Cannot list directory %s'%start)
else: # otherwise, just re-raise the error so that it propagates
raise
total = 0L
for item in dir_list:
# Get statistics on each item--file and subdirectory--of start
path = join(start, item)
try: stats = os.stat(path)
except:
if not skip_errs:
raise DirSizeError('Cannot stat %s'%path) #译注:没有读权限
# The size in bytes is in the seventh item of the stats tuple, so:
total += stats[6]
# recursive descent if warranted
if isdir(path) and (follow_links or not islink(path)): #译注:遍历计算子目录
bytes = dir_size(path, follow_links, start_depth+1, max_depth)
total += bytes
if max_depth and (start_depth < max_depth):
print_path(path, bytes, units)
return total
def print_path(path, bytes, units='b'):
if units == 'k':
print '%-8ld%s' % (bytes / 1024, path)
elif units == 'm':
print '%-5ld%s' % (bytes / 1024 / 1024, path)
else:
print '%-11ld%s' % (bytes, path)
def usage (name):
print "usage: %s [-bkLm] [-d depth] directory [directory...]" % name
print '/t-b/t/tDisplay in Bytes (default)'
print '/t-k/t/tDisplay in Kilobytes'
print '/t-m/t/tDisplay in Megabytes'
print '/t-L/t/tFollow symbolic links (meaningful on Unix only)'
print '/t-d, --depth/t# of directories down to print (default = 0)'
if __name__=='__main__':
# When used as a script:
import string, sys, getopt
units = 'b'
follow_links = 0
depth = 0
try:
opts, args = getopt.getopt(sys.argv[1:], "bkLmd:", ["depth="]) #译注:解析命令行参数
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(1)
for o, a in opts:
if o == '-b': units = 'b'
elif o == '-k': units = 'k'
elif o == '-L': follow_links = 1
elif o == '-m': units = 'm'
elif o in ('-d', '--depth'):
try: depth = int(a)
except:
print "Not a valid integer: (%s)" % a
usage(sys.argv[0])
sys.exit(1)
if len(args) < 1:
print "No directories specified"
usage(sys.argv[0])
sys.exit(1)
else:
paths = args
for path in paths:
try: bytes = dir_size(path, follow_links, 0, depth)
except DirSizeError, x: print "Error:", x
else: print_path(path, bytes,units)
- dir_size.py
- py
- py
- py
- py
- py
- py
- py
- Py
- bin2hex.py && hex2bin.py
- web.py (url.py)
- [py]py存放家具
- Xctf之调皮的py-py-py
- sendEmail.py
- Html.py
- web.py
- mail.py
- sendmail.py
- 告诉你为何人民币升值国内物价却飞涨
- C#设计模式(1)
- 图形图像处理-之-误差扩散 上篇
- sscanf() 函数用法详解
- Pandora's Box
- dir_size.py
- JAVA实现邮箱上传文件功能
- Greek Myths
- MP4格式转换为AMV格式
- PictureBox显示图片时提示参数错误
- 在项目中使用设计模式思想——Abstract Factory、Factory Method、Singleton和Strategy模式的使用(上)
- ‘珍爱生命,远离偶像’
- C# .NET 如何在DataGridView中最简洁的实现对单元格显示的处理
- LDAP资料