Skip to content

Commit c9e6e51

Browse files
committed
Add line_count and character counting capability
1 parent 544988d commit c9e6e51

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

fileinfo.py

+32-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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 :
77

8-
# Description : Show file information for a given file
8+
# Description : Show file information for a given file
99

1010

1111
# get file information using os.stat()
@@ -19,10 +19,18 @@
1919
try_count = 16
2020

2121
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)
2330
try_count >>= 1
2431
try:
2532
file_stats = os.stat(file_name)
33+
print ("This is os.stat",file_stats)
2634
break
2735
except OSError:
2836
print ("\nNameError : [%s] No such file or directory\n", file_name)
@@ -40,25 +48,29 @@
4048
'f_la' : time.strftime("%d/%m/%Y %I:%M:%S %p",
4149
time.localtime(file_stats[stat.ST_ATIME])),
4250
'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
4454
}
4555

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'])
5163

5264
if stat.S_ISDIR(file_stats[stat.ST_MODE]):
5365
print ("This a directory")
5466
else:
5567
print ("This is not a directory\n")
5668
print ("A closer look at the os.stat(%s) tuple:" % file_name)
5769
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)"""
6476
)

0 commit comments

Comments
 (0)