#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Doubt on Dictionary and Json


    I wrote a python script that compares two lists and outputs the common elements. Assume these elements to be filenames in some directory. I am trying collect data like size, last modified time, path, getting the result for each filename in list of
    filenames. My aim is to add all the data to a dictionary where filename will be the key and size, last modified time, path will be values. Want to write all the common filenames with the data to JSON file.

    Problem:
    while printing I am getting all the output filenames but while writing to JSON file, it is getting overwritten and I am getting the last filename and its data.

    Can any one suggest the possible reason.

    Code snippet:
    for path, dirs, files in os.walk(Directory):
    R = cmp(files2, files)
    R =list(set(files2).intersection(set(files)))
    for X in R:
    pathX = os.path.join(path, X)

    size = os.path.getsize(pathX)

    mtime = os.path.getmtime(pathX)
    time = datetime.fromtimestamp(mtime).strftime("%d%b%Y")
    print X,"", time,"", pathX,"", size,"bytes"

    X_Json=dict.fromkeys([X],[time,pathX,size])
    print X_Json
    ResultsJson = json.dumps(X_Json)
    open('results.json', 'wb').write(ResultsJson)
    ##Consider correct indentation
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Can you put your code in [ code ] tags to preserve proper indentation, etc.?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    enabling those who don't [[u][/u]code]


    You probably will want to open the file just once, write the data, then close the file. The file context using the with statement handles closing the file for you. The point is that every time you called open(name,'wb') you create a new file, clobbering whatever was there before. Even if it was only a millisecond ago.
    Code:
    with open('results.json', 'wb') as jsonresults:
        for path, dirs, files in os.walk(Directory):
            R = cmp(files2, files)
            R =list(set(files2).intersection(set(files)))                 
            for X in R:
                pathX = os.path.join(path, X)
                
                size = os.path.getsize(pathX)
                
                mtime = os.path.getmtime(pathX)
                time =   datetime.fromtimestamp(mtime).strftime("%d%b%Y")
                print X,"", time,"", pathX,"", size,"bytes"
                
                X_Json=dict.fromkeys([X],[time,pathX,size])
                print X_Json
                ResultsJson = json.dumps(X_Json)
                jsonresults.write(ResultsJson)
    Last edited by b49P23TIvg; August 29th, 2013 at 09:26 PM. Reason: insert a quote
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Front-end integration with the back-end code


    Thank you all..

    Now I have another query, need some suggestion. I am trying to build a search engine. I want to integrate the front-end part with the back-end code(python script-which gives the output). So when I give a query on the web page, the python script will run and give the output on the webpage.

    Any suggestions how to proceed??
    Link to any tutorial and docs will be a great help.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    I'd recommend looking at some simple web frameworks to start with, like Flask, web.py or maybe even itty. It's pretty trivial to use something like that to interface with any kind of Python back-end script.

IMN logo majestic logo threadwatch logo seochat tools logo