|
1 |
| -# Script Name : fileinfo.py |
2 |
| -# Author : Not sure where I got this from |
3 |
| -# Created : 28th November 2011 |
4 |
| -# Last Modified : |
5 |
| -# Version : 1.0 |
6 |
| -# Modifications : |
| 1 | +# Script Name : fileinfo.py |
| 2 | +# Author : Not sure where I got this from |
| 3 | +# Created : 28th November 2011 |
| 4 | +# Last Modified : |
| 5 | +# Version : 1.0 |
| 6 | +# Modifications : |
7 | 7 |
|
8 |
| -# Description : Show file information for a given file |
| 8 | +# Description : Show file information for a given file |
9 | 9 |
|
10 | 10 |
|
11 | 11 | # get file information using os.stat()
|
|
19 | 19 | try_count = 16
|
20 | 20 |
|
21 | 21 | while try_count:
|
22 |
| - file_name = raw_input("Enter a file name: ") # pick a file you have |
| 22 | + file_name = input("Enter a file name: ") # pick a file you have |
| 23 | + fhand = open(file_name) |
| 24 | + count = 0 |
| 25 | + for lines in fhand: |
| 26 | + count = count + 1 |
| 27 | + fhand = open(file_name) |
| 28 | + inp = fhand.read() |
| 29 | + t_char = len(inp) |
23 | 30 | try_count >>= 1
|
24 | 31 | try:
|
25 | 32 | file_stats = os.stat(file_name)
|
| 33 | + print ("This is os.stat",file_stats) |
26 | 34 | break
|
27 | 35 | except OSError:
|
28 | 36 | print ("\nNameError : [%s] No such file or directory\n", file_name)
|
|
40 | 48 | 'f_la' : time.strftime("%d/%m/%Y %I:%M:%S %p",
|
41 | 49 | time.localtime(file_stats[stat.ST_ATIME])),
|
42 | 50 | 'f_ct' : time.strftime("%d/%m/%Y %I:%M:%S %p",
|
43 |
| - time.localtime(file_stats[stat.ST_CTIME])) |
| 51 | + time.localtime(file_stats[stat.ST_CTIME])), |
| 52 | + 'no_of_lines':count, |
| 53 | + 't_char':t_char |
44 | 54 | }
|
45 | 55 |
|
46 |
| -print ("\nfile name = %(fname)s", file_info) |
47 |
| -print ("file size = %(fsize)s bytes", file_info) |
48 |
| -print ("last modified = %(f_lm)s", file_info) |
49 |
| -print ("last accessed = %(f_la)s", file_info) |
50 |
| -print ("creation time = %(f_ct)s\n", file_info) |
| 56 | +print ("\nfile name =", file_info['fname']) |
| 57 | +print ("file size =", file_info['fsize'] , "bytes") |
| 58 | +print ("last modified =", file_info['f_lm']) |
| 59 | +print ("last accessed =", file_info['f_la']) |
| 60 | +print ("creation time =", file_info['f_ct']) |
| 61 | +print ("Total number of lines are =", file_info['no_of_lines']) |
| 62 | +print ("Total number of characters are =", file_info['t_char']) |
51 | 63 |
|
52 | 64 | if stat.S_ISDIR(file_stats[stat.ST_MODE]):
|
53 | 65 | print ("This a directory")
|
54 | 66 | else:
|
55 | 67 | print ("This is not a directory\n")
|
56 | 68 | print ("A closer look at the os.stat(%s) tuple:" % file_name)
|
57 | 69 | print (file_stats)
|
58 |
| - print ("\nThe above tuple has the following sequence:") |
59 |
| - print ("""st_mode (protection bits), st_ino (inode number), |
60 |
| - st_dev (device), st_nlink (number of hard links), |
61 |
| - st_uid (user ID of owner), st_gid (group ID of owner), |
62 |
| - st_size (file size, bytes), st_atime (last access time, seconds since epoch), |
63 |
| - st_mtime (last modification time), st_ctime (time of creation, Windows)""" |
| 70 | + print ("\nThe above tuple has the following sequence: ") |
| 71 | + print ("""st_mode (protection bits), st_ino (inode number), |
| 72 | + st_dev (device), st_nlink (number of hard links), |
| 73 | + st_uid (user ID of owner), st_gid (group ID of owner), |
| 74 | + st_size (file size, bytes), st_atime (last access time, seconds since epoch), |
| 75 | + st_mtime (last modification time), st_ctime (time of creation, Windows)""" |
64 | 76 | )
|
0 commit comments