i have a bunch of xml files under a directory. I am trying to fetch some details out of the files such as some tag values and its last modified date and time like that. My script is
from xml.dom.minidom import parse, parseString
import os, stat
import sys
import datetime
def shahul(dir):
for r,d,f in os.walk(dir):
for files in f:
if files.endswith(".xml"):
dom=parse(os.path.join(r, files))
info=os.stat(os.path.join(r, files));
atime=info.st_atime;
ctime=info.st_ctime;
mtime=info.st_mtime;
name = dom.getElementsByTagName('rev')
title = dom.getElementsByTagName('title')
approved_by = dom.getElementsByTagName('approved-by')
approved = approved_by[0].attributes['approved'].value
print (files,
datetime.datetime.fromtimestamp(atime),
datetime.datetime.fromtimestamp(ctime),
datetime.datetime.fromtimestamp(mtime),
title[0].firstChild.nodeValue,
name[0].firstChild.nodeValue,
approved, sep='\t')
shahul("location")
Everything works fine. But the time is not valid. It shows same time for last creation time, modified time. Also in wrong format, i dont know why it is showing like this. I got the output like.
XXX.xml 05:49.2 05:49.2 05:49.2 File_name E yes
XXX.xml 22:14.6 22:14.6 22:14.6 File_name F yes
XXX.xml 29:15.5 29:15.5 29:15.5 File_name E yes
XXX.xml 16:02.1 16:02.1 16:02.1 File_name F2 yes
XXX.xml 00:48.0 00:48.0 00:48.0 File_name B1 yes
It seems not right. how to correct it?
st_ctime
is not creation time, it is change time. You also may want to format the datetime objects using .isoformat
or something similar.
This might shed some light on on atime, ctime, and mtime.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.