Skip to content

Commit 3f6cc4d

Browse files
committed
This display information about the file you give it
1 parent e20b3b4 commit 3f6cc4d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

fileinfo.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# get file information using os.stat()
2+
# tested with Python24 vegsaeat 25sep2006
3+
import os
4+
import stat # index constants for os.stat()
5+
import time
6+
# pick a file you have ...
7+
#file_name = 'puttylogs.py'
8+
file_name = raw_input("Enter a file name: ")
9+
file_stats = os.stat(file_name)
10+
# create a dictionary to hold file info
11+
file_info = {
12+
'fname': file_name,
13+
'fsize': file_stats [stat.ST_SIZE],
14+
'f_lm': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_MTIME])),
15+
'f_la': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_ATIME])),
16+
'f_ct': time.strftime("%d/%m/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME]))
17+
}
18+
print
19+
print "file name = %(fname)s" % file_info
20+
print "file size = %(fsize)s bytes" % file_info
21+
print "last modified = %(f_lm)s" % file_info
22+
print "last accessed = %(f_la)s" % file_info
23+
print "creation time = %(f_ct)s" % file_info
24+
print
25+
if stat.S_ISDIR(file_stats[stat.ST_MODE]):
26+
print "This a directory"
27+
else:
28+
print "This is not a directory"
29+
print
30+
print "A closer look at the os.stat(%s) tuple:" % file_name
31+
print file_stats
32+
print
33+
print "The above tuple has the following sequence:"
34+
print """st_mode (protection bits), st_ino (inode number),
35+
st_dev (device), st_nlink (number of hard links),
36+
st_uid (user ID of owner), st_gid (group ID of owner),
37+
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
38+
st_mtime (last modification time), st_ctime (time of creation, Windows)"""

0 commit comments

Comments
 (0)