From c9e6e5122a7601d3e98d53e53a24c76e8e20431b Mon Sep 17 00:00:00 2001 From: sandynigs Date: Tue, 11 Jul 2017 12:48:02 +0530 Subject: [PATCH] Add line_count and character counting capability --- fileinfo.py | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/fileinfo.py b/fileinfo.py index d5c200b9e52..72a08709367 100644 --- a/fileinfo.py +++ b/fileinfo.py @@ -1,11 +1,11 @@ -# Script Name : fileinfo.py -# Author : Not sure where I got this from -# Created : 28th November 2011 -# Last Modified : -# Version : 1.0 -# Modifications : +# Script Name : fileinfo.py +# Author : Not sure where I got this from +# Created : 28th November 2011 +# Last Modified : +# Version : 1.0 +# Modifications : -# Description : Show file information for a given file +# Description : Show file information for a given file # get file information using os.stat() @@ -19,10 +19,18 @@ try_count = 16 while try_count: - file_name = raw_input("Enter a file name: ") # pick a file you have + file_name = input("Enter a file name: ") # pick a file you have + fhand = open(file_name) + count = 0 + for lines in fhand: + count = count + 1 + fhand = open(file_name) + inp = fhand.read() + t_char = len(inp) try_count >>= 1 try: file_stats = os.stat(file_name) + print ("This is os.stat",file_stats) break except OSError: print ("\nNameError : [%s] No such file or directory\n", file_name) @@ -40,14 +48,18 @@ 'f_la' : time.strftime("%d/%m/%Y %I:%M:%S %p", time.localtime(file_stats[stat.ST_ATIME])), 'f_ct' : time.strftime("%d/%m/%Y %I:%M:%S %p", - time.localtime(file_stats[stat.ST_CTIME])) + time.localtime(file_stats[stat.ST_CTIME])), + 'no_of_lines':count, + 't_char':t_char } -print ("\nfile name = %(fname)s", file_info) -print ("file size = %(fsize)s bytes", file_info) -print ("last modified = %(f_lm)s", file_info) -print ("last accessed = %(f_la)s", file_info) -print ("creation time = %(f_ct)s\n", file_info) +print ("\nfile name =", file_info['fname']) +print ("file size =", file_info['fsize'] , "bytes") +print ("last modified =", file_info['f_lm']) +print ("last accessed =", file_info['f_la']) +print ("creation time =", file_info['f_ct']) +print ("Total number of lines are =", file_info['no_of_lines']) +print ("Total number of characters are =", file_info['t_char']) if stat.S_ISDIR(file_stats[stat.ST_MODE]): print ("This a directory") @@ -55,10 +67,10 @@ print ("This is not a directory\n") print ("A closer look at the os.stat(%s) tuple:" % file_name) print (file_stats) - print ("\nThe above tuple has the following sequence:") - print ("""st_mode (protection bits), st_ino (inode number), - st_dev (device), st_nlink (number of hard links), - st_uid (user ID of owner), st_gid (group ID of owner), - st_size (file size, bytes), st_atime (last access time, seconds since epoch), - st_mtime (last modification time), st_ctime (time of creation, Windows)""" + print ("\nThe above tuple has the following sequence: ") + print ("""st_mode (protection bits), st_ino (inode number), + st_dev (device), st_nlink (number of hard links), + st_uid (user ID of owner), st_gid (group ID of owner), + st_size (file size, bytes), st_atime (last access time, seconds since epoch), + st_mtime (last modification time), st_ctime (time of creation, Windows)""" )