|
13 | 13 | from __future__ import print_function |
14 | 14 | import os |
15 | 15 | import sys |
16 | | -import stat # index constants for os.stat() |
| 16 | +import stat # index constants for os.stat() |
17 | 17 | import time |
18 | 18 |
|
19 | 19 | try_count = 16 |
20 | 20 | while try_count: |
21 | | - file_name = raw_input("Enter a file name: ") # pick a file you have ... |
| 21 | + file_name = raw_input("Enter a file name: ") # pick a file you have |
22 | 22 | try_count >>= 1 |
23 | | - try : |
| 23 | + try: |
24 | 24 | file_stats = os.stat(file_name) |
25 | 25 | break |
26 | 26 | except OSError: |
27 | | - print ("\nNameError : [%s] No such file or directory\n" %file_name) |
| 27 | + print ("\nNameError : [%s] No such file or directory\n", file_name) |
28 | 28 |
|
29 | 29 | if try_count == 0: |
30 | 30 | print ("Trial limit exceded \nExiting program") |
31 | 31 | sys.exit() |
32 | 32 | # create a dictionary to hold file info |
33 | 33 | file_info = { |
34 | | - 'fname': file_name, |
35 | | - 'fsize': file_stats [stat.ST_SIZE], |
36 | | - 'f_lm': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_MTIME])), |
37 | | - 'f_la': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_ATIME])), |
38 | | - 'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME])) |
| 34 | + 'fname': file_name, |
| 35 | + 'fsize': file_stats[stat.ST_SIZE], |
| 36 | + 'f_lm': time.strftime("%d/%m/%Y %I:%M:%S %p", |
| 37 | + time.localtime(file_stats[stat.ST_MTIME])), |
| 38 | + 'f_la': time.strftime("%d/%m/%Y %I:%M:%S %p", |
| 39 | + time.localtime(file_stats[stat.ST_ATIME])), |
| 40 | + 'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p", |
| 41 | + time.localtime(file_stats[stat.ST_CTIME])) |
39 | 42 | } |
| 43 | + |
40 | 44 | print |
41 | | -print ("file name = %(fname)s" % file_info) |
42 | | -print ("file size = %(fsize)s bytes" % file_info) |
43 | | -print ("last modified = %(f_lm)s" % file_info) |
44 | | -print ("last accessed = %(f_la)s" % file_info) |
45 | | -print ("creation time = %(f_ct)s" % file_info) |
| 45 | +print ("file name = %(fname)s", file_info) |
| 46 | +print ("file size = %(fsize)s bytes", file_info) |
| 47 | +print ("last modified = %(f_lm)s", file_info) |
| 48 | +print ("last accessed = %(f_la)s", file_info) |
| 49 | +print ("creation time = %(f_ct)s", file_info) |
46 | 50 | print |
47 | 51 | if stat.S_ISDIR(file_stats[stat.ST_MODE]): |
48 | | - print ("This a directory") |
| 52 | + |
| 53 | + print ("This a directory") |
49 | 54 | else: |
50 | | - print ("This is not a directory") |
51 | | - print () |
52 | | - print ("A closer look at the os.stat(%s) tuple:" % file_name) |
53 | | - print (file_stats) |
54 | | - print () |
55 | | - print ("The above tuple has the following sequence:") |
56 | | - print ("""st_mode (protection bits), st_ino (inode number), |
57 | | - st_dev (device), st_nlink (number of hard links), |
58 | | - st_uid (user ID of owner), st_gid (group ID of owner), |
59 | | - st_size (file size, bytes), st_atime (last access time, seconds since epoch), |
60 | | - st_mtime (last modification time), st_ctime (time of creation, Windows)""" |
| 55 | + print ("This is not a directory") |
| 56 | + print () |
| 57 | + print ("A closer look at the os.stat(%s) tuple:" % file_name) |
| 58 | + print (file_stats) |
| 59 | + print () |
| 60 | + print ("The above tuple has the following sequence:") |
| 61 | + print ("""st_mode (protection bits), st_ino (inode number), |
| 62 | + st_dev (device), st_nlink (number of hard links), |
| 63 | + st_uid (user ID of owner), st_gid (group ID of owner), |
| 64 | + st_size (file size, bytes), st_atime (last access time, seconds since epoch), |
| 65 | + st_mtime (last modification time), st_ctime (time of creation, Windows)""" |
61 | 66 | ) |
0 commit comments