1#! /usr/bin/env python3 2 3# Print the product of age and size of each file, in suitable units. 4# 5# Usage: byteyears [ -a | -m | -c ] file ... 6# 7# Options -[amc] select atime, mtime (default) or ctime as age. 8 9import sys, os, time 10from stat import * 11 12def main(): 13 14 # Use lstat() to stat files if it exists, else stat() 15 try: 16 statfunc = os.lstat 17 except AttributeError: 18 statfunc = os.stat 19 20 # Parse options 21 if sys.argv[1] == '-m': 22 itime = ST_MTIME 23 del sys.argv[1] 24 elif sys.argv[1] == '-c': 25 itime = ST_CTIME 26 del sys.argv[1] 27 elif sys.argv[1] == '-a': 28 itime = ST_CTIME 29 del sys.argv[1] 30 else: 31 itime = ST_MTIME 32 33 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor 34 now = time.time() # Current time, for age computations 35 status = 0 # Exit status, set to 1 on errors 36 37 # Compute max file name length 38 maxlen = 1 39 for filename in sys.argv[1:]: 40 maxlen = max(maxlen, len(filename)) 41 42 # Process each argument in turn 43 for filename in sys.argv[1:]: 44 try: 45 st = statfunc(filename) 46 except OSError as msg: 47 sys.stderr.write("can't stat %r: %r\n" % (filename, msg)) 48 status = 1 49 st = () 50 if st: 51 anytime = st[itime] 52 size = st[ST_SIZE] 53 age = now - anytime 54 byteyears = float(size) * float(age) / secs_per_year 55 print(filename.ljust(maxlen), end=' ') 56 print(repr(int(byteyears)).rjust(8)) 57 58 sys.exit(status) 59 60if __name__ == '__main__': 61 main() 62