Python sqlite3 and EasyGUI: Using Python to read files in a folder or multiple folders, then Add new gene of the folder and file name of the database?

I am new in python and am trying to add folder names and text files in those folders to the database. The problem is that i don’t know how to add the “textfiles” and “opendirectory” to the database. Please look at this code and help me. Thanks

    #!/usr/bin/python

    from easygui import *
    import sys, glob, os, sqlite3

    msgbox("Please choose your folder ","Welcome to MTT", ok_button ="Choose")

    opendirectory = diropenbox("Welcome", "MTT",None)

    con = sqlite3.connect('test.db')
    cur = con.cursor()
    cur.execute('DROP TABLE IF EXISTS data ')
    cur.execute('CREATE TABLE folder( folderid INTEGER PRIMARY KEY, foldername   VARCHAR(120))')
    cur.execute('CREATE TABLE file( fileid INTEGER PRIMARY KEY, folderid INTEGER, dataname VARCHAR(120) FOREIGN KEY(folderid) REFERENCES foldername(folderid))')
    con.commit()

    def main():

        for dirpath,dirnames,filenames in os.walk(opendirectory):

            for textfiles in filenames:

                print textfiles
                print opendirectory
                cur.execute ('INSERT INTO folder (folderid, foldername) VALUES (null,opendirector)')
                cur.execute('INSERT INTO file(fileid, name) VALUES(null,textfiles)')
                cur.execute('SELECT * FROM data')
                print cur.fetchall()

    main()

    print 'success'
This entry was posted in Codes & Scripts and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>