|
1 | 1 | # Script Name : fileinfo.py
|
2 | 2 | # Author : Not sure where I got this from
|
3 | 3 | # Created : 28th November 2011
|
4 |
| -# Last Modified : |
| 4 | +# Last Modified : |
5 | 5 | # Version : 1.0
|
6 |
| -# Modifications : |
| 6 | +# Modifications : |
7 | 7 |
|
8 | 8 | # Description : Show file information for a given file
|
9 | 9 |
|
10 | 10 |
|
11 | 11 | # get file information using os.stat()
|
12 | 12 | # tested with Python24 vegsaeat 25sep2006
|
| 13 | +from __future__ import print_function |
13 | 14 | import os
|
| 15 | +import sys |
14 | 16 | import stat # index constants for os.stat()
|
15 | 17 | import time
|
16 |
| -# pick a file you have ... |
17 |
| -file_name = raw_input("Enter a file name: ") |
18 |
| -file_stats = os.stat(file_name) |
| 18 | + |
| 19 | +try_count = 16 |
| 20 | +while try_count: |
| 21 | + file_name = raw_input("Enter a file name: ") # pick a file you have ... |
| 22 | + try_count >>= 1 |
| 23 | + try : |
| 24 | + file_stats = os.stat(file_name) |
| 25 | + break |
| 26 | + except OSError: |
| 27 | + print ("\nNameError : [%s] No such file or directory\n" %file_name) |
| 28 | + |
| 29 | +if try_count == 0: |
| 30 | + print ("Trial limit exceded \nExiting program") |
| 31 | + sys.exit() |
19 | 32 | # create a dictionary to hold file info
|
20 | 33 | file_info = {
|
21 | 34 | 'fname': file_name,
|
|
25 | 38 | 'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME]))
|
26 | 39 | }
|
27 | 40 | print
|
28 |
| -print "file name = %(fname)s" % file_info |
29 |
| -print "file size = %(fsize)s bytes" % file_info |
30 |
| -print "last modified = %(f_lm)s" % file_info |
31 |
| -print "last accessed = %(f_la)s" % file_info |
32 |
| -print "creation time = %(f_ct)s" % file_info |
| 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) |
33 | 46 | print
|
34 | 47 | if stat.S_ISDIR(file_stats[stat.ST_MODE]):
|
35 |
| - print "This a directory" |
| 48 | + print ("This a directory") |
36 | 49 | else:
|
37 |
| - print "This is not a directory" |
38 |
| - print |
39 |
| - print "A closer look at the os.stat(%s) tuple:" % file_name |
40 |
| - print file_stats |
41 |
| - print |
42 |
| - print "The above tuple has the following sequence:" |
43 |
| - print """st_mode (protection bits), st_ino (inode number), |
| 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), |
44 | 57 | st_dev (device), st_nlink (number of hard links),
|
45 | 58 | st_uid (user ID of owner), st_gid (group ID of owner),
|
46 | 59 | st_size (file size, bytes), st_atime (last access time, seconds since epoch),
|
47 | 60 | st_mtime (last modification time), st_ctime (time of creation, Windows)"""
|
| 61 | +) |
0 commit comments