Create Dict within list of dict

前端 未结 2 1824
挽巷
挽巷 2021-01-28 11:26

I\'m trying to create a dict within a list of dicts. How do I build the data structure and later to fetch the data via jinja2? Here is an example:

var = {
    \         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 12:12

    Not sure what do you mean by 'site' ...

    The code below uses site as location on the file system. It iterates over the sites list and read the files for each site.

    import os
    import datetime
    
    data = dict()
    
    sites = ['.']
    for site in sites:
        data['listofiles'] = []
        data['site'] = site
        for f in os.listdir(site):
            data['listofiles'].append(
                {'time': str(datetime.datetime.fromtimestamp(os.path.getmtime(os.path.join(site, f)))), 'name': f})
    

提交回复
热议问题