// Source: https://stackoverflow.com/a/1019572
count = (sum(1 for line in f))
f.seek(0)
t_char = (sum([len(line) for line in f]))
except FileNotFoundError as e:
print(e)
sys.exit(1)
// When open item is a directory (python2)
except IOError:
pass
// When open item is a directory (python3)
except IsADirectoryError:
pass
file_stats = os.stat(file_name)
// create a dictionary to hold file info
file_info = {
"fname": file_name,
"fsize": file_stats[stat.ST_SIZE],
"f_lm": time.strftime("%d/%m/%Y %I:%M:%S %p",
time.localtime(file_stats[stat.ST_MTIME])),
"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])),
"no_of_lines": count,
"t_char": t_char
}
file_info_fmt = ("\nfile name = {}\nfile size = {}\nlast modified = {}\n"
"last accessed = {}\ncreation time = {}\n"
"Total number of lines are = {}\n"
"Total number of characters are = {}")
print(
file_info_fmt.format(file_info["fname"],
str(file_info["fsize"]) + " bytes", file_info["f_lm"],
file_info["f_la"], file_info["f_ct"],
file_info["no_of_lines"], file_info["t_char"]))