I want to modify this python script to output the modification date of the parsed file along with it's title
Question: So the last programmer left me with this script, that grabs all the old
content and writes it's all out as a "partial' file.. which strips out all the
container html and leave's just the html of the article itself. It then write
a manifest as JSON which give the url, the title with the date.

I'd like it to order all the files by the date created, in the manifest, and
in the filenames created. (something like [unix-create-date]-[url].partial]).
Then i can use the manifest file to list the file in the order they were
created, and use the file names themselves as well.

The purpose of all this was to scan the old archives of an old web site and
set the up for insertion into the new web sites template. However the current
list of articles is not by date. I needs to be. Getting the manifest to output
the files by date would do it.

I don't know python at all. So i have no idea how to get the modification date
in there. Seems to be there has got to be a way to open the files in the path
ordered by create date. Thanks for any responses!

Here is the full script.

    
    
    #!/usr/bin/python
    
    import os
    import re
    from BeautifulSoup import BeautifulSoup
    import simplejson as json
    
    def parse_article(root, filename):
        path = os.path.join(root, filename)
        abs_path = os.path.abspath(path)
        try:
            article = open(abs_path, 'rU')
            html = article.read()
            article.close()
        except IOError:
            print "Cannot open article: %s" % path
    
        url = "/%s" % path
        soup = BeautifulSoup(html)
    
        title = None
        fallbacks = ['h1', 'h2', 'h3', 'title']
        for fallback in fallbacks:
            if title is None:
                title = soup.find(fallback)
            else:
                break
    
        content = u"" if soup.body is None else soup.body.renderContents()
        save_file(root, "%s.partial" % filename, content)
    
        title = u"" if title is None else title.renderContents()
        return unicode(url), title
    
    def process_folder(path):
        files = os.listdir(path)
        articles = filter(lambda name: not name.startswith('index.') and (name.endswith('.html') or name.endswith('.htm')), files)
        manifest = {}
    
        for article in articles:
            url, title = parse_article(path, article)
            manifest[url] = title
    
        return manifest
    
    def save_json(root, name, obj):
        if len(obj.keys()) == 0:
            return
    
        path = os.path.join(root, name)
        manifest = open(path, 'w')
        json.dump(obj, manifest)
        manifest.close()
        print "Wrote %s" % path
    
    def save_file(root, name, content):
        path = os.path.join(root, name)
        manifest = open(path, 'w')
        manifest.write(content)
        manifest.close()
        print "Wrote %s" % path
    
    def process(root):
        root = os.path.abspath(root)
        root_re = '^%s[/]*' % root
        for dirname, dirnames, filenames in os.walk(root):
            dirname = re.sub(root_re, '', dirname)
            if len(dirname) > 0:
                manifest = process_folder(dirname)
                abs_path = os.path.abspath(os.path.join(root, dirname))
                save_json(abs_path, "manifest.json", manifest)
    
    
    if __name__ == "__main__":
        process('.')
    


Answer: You can get the modification date of a file by using
[`os.stat`](https://docs.python.org/3/library/os.html#os.stat):

    
    
    >>> import os, time
    >>> result = os.stat("/tmp/z.py")
    >>> result
    posix.stat_result(st_mode=33188, st_ino=6034492, st_dev=16777220L, st_nlink=1, st_uid=501, st_gid=0, st_size=189, st_atime=1424735724, st_mtime=1424735651, st_ctime=1424735651)
    >>> print "Modification date: %s -> %s" % (result.st_mtime, time.ctime(result.st_mtime))
    Modification date: 1424735651.0 -> Mon Feb 23 15:54:11 2015
    >>> print "Creation date: %s -> %s" % (result.st_ctime, time.ctime(result.st_ctime))
    Creation date: 1424735651.0 -> Mon Feb 23 15:54:11 2015
    >>> print "Access date: %s -> %s" % (result.st_atime, time.ctime(result.st_atime))
    Access date: 1424735724.0 -> Mon Feb 23 15:55:24 2015
    

So in your code, you'd probably want to store it in the manifest around here:

    
    
        ...
        for article in articles:
            url, title = parse_article(path, article)
            manifest[url] = title
            manifest[ctime] = os.stat(path).st_ctime
        ...
    

With that information you can then sort the files based on `ctime`, or convert
it to a `datetime` object, etc.

