Nearby lessons
102 of 108Python - Dates and Time
Detailed Tutorial Notes
How to get information about a File:
We can get statistics of a file like size, last accessed time,last modified time etc by using
stat() function of os module.
stats = os.stat("abc.txt")
The statistics of a file includes the following parameters:
st_mode==>Protection Bits
st_ino==>Inode number
st_dev===>device
st_nlink===>no of hard links
st_uid===>userid of owner
st_gid==>group id of owner
st_size===>size of file in bytes
st_atime==>Time of most recent access
st_mtime==>Time of Most recent modification
st_ctime==> Time of Most recent meta data changeNote:
st_atime, st_mtime and st_ctime returns the time as number of milli seconds since Jan 1st
1970 ,12:00AM. By using datetime module fromtimestamp() function,we can get exact
date and time.
Q. To print all statistics of file abc.txt:
main.py
os.stat_result(st_mode=33206, st_ino=844424930132788, st_dev=2657980798, st_nlin k=1, st_uid=0, st_gid=0, st_size=22410, st_atime=1505451446, st_mtime=1505538999 , st_ctime=1505451446)
Q. To print specified properties:
main.py
File Size in Bytes: 22410 File Last Accessed Time: 2017-09-15 10:27:26.599490 File Last Modified Time: 2017-09-16 10:46:39.245394
Pickling and Unpickling of Objects:
Sometimes we have to write total state of object to the file and we have to read total
object from the file.
The process of writing state of object to the file is called pickling and the process of
reading state of an object from the file is called unpickling.
We can implement pickling and unpickling by using pickle module of Python.
pickle module contains dump() function to perform pickling.
pickle.dump(object,file)
pickle module contains load() function to perform unpickling
obj=pickle.load(file)
e2
eno: 100
ename: Durga
esal: 10000
eaddr: HYD
abc.txt
pickle.dump
(e1, f)
pickling
pickle load (f)
unpickling
eno: 100
ename: Durga
esal: 10000
eaddr: HYD
eno: 100
ename: Durga
esal: 10000
eaddr: HYD
e1