Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6844061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:19:47+00:00 2026-05-27T00:19:47+00:00

I am new in python and am trying to add folder names and text

  • 0

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 folder')
cur.execute('DROP TABLE IF EXISTS file')
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, dataname) VALUES(null,textfiles)')
            cur.execute('SELECT * FROM folder')
            print cur.fetchall()


main()

print 'success'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:19:48+00:00Added an answer on May 27, 2026 at 12:19 am

    Assuming that you want to add all the filenames to the database (without considering their relative path inside opendirectory), here is a way to correct your queries.

    cur.execute ("INSERT INTO folder (foldername) VALUES (?);", (opendirectory))
    cur.execute("INSERT INTO file (dataname) VALUES(?);", (textfiles))
    

    NB: this won’t be sufficient to make the logical link in database between a file and the opendirectory it was found in.


    Now, let’s say you want to store in DB the path of the parent folder of the file in addition to its filename:
    Just add a column parent_folder in your file table, and use an insert query like this (I changed the variable names to make them easier to understand):

    for dirpath, dirsInDirpath, filesInDirPath in os.walk(opendirectory):
        for myfile in filesInDirPath:   
            cur.execute("INSERT INTO file (dataname, parent_folder) VALUES(?, ?);", (myfile, dirpath))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to python, and I'm trying to get to know the list comprehensions
So, I know some Python but I thought that I should try to add
Hi I'm new to python. I am trying to add different key value pairs
Im new two python and am trying to grow a dictionary of dictionaries. I
I'm brand new at Python and I'm trying to write an extension to an
I'm new to python but I've run into a hitch when trying to implement
I am pretty new to python and working with firmata I am trying to
I'm really new to Python. I'm trying to import a third party module called
I am very new to Python, and trying to figure out how to create
i'm new with python.. Actually, i'm trying to send featured email with python: html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.