简体   繁体   English

Python,easyGUI和sqlite3:使用python读取一个文件夹或多个文件夹中的文件,然后将文件夹名称和文件名添加到数据库

[英]Python, easyGUI and sqlite3: Using python to read files in one folder or multiple folder, then adding the folder names and filenames to database

I am new in python and am trying to add folder names and text files in those folders to the database. 我是python中的新手,我正在尝试将这些文件夹中的文件夹名称和文本文件添加到数据库中。 The problem is that i don't know how to add the "textfiles" and "opendirectory" to the database. 问题是我不知道如何将“textfiles”和“opendirectory”添加到数据库中。 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'

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. 假设您要将所有文件名添加到数据库中(不考虑它们在opendirectory的相对路径),这是一种纠正查询的方法。

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. 注意:这不足以在数据库中创建文件和opendirectory它的opendirectory之间的逻辑链接。


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): 现在,假设您希望在DB中存储文件的父文件夹的路径以及文件名:只需在file表中添加一个列parent_folder ,并使用这样的插入查询(我更改了变量名称他们更容易理解):

for dirpath, dirsInDirpath, filesInDirPath in os.walk(opendirectory):
    for myfile in filesInDirPath:   
        cur.execute("INSERT INTO file (dataname, parent_folder) VALUES(?, ?);", (myfile, dirpath))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python / Pandas-根据文件名中的日期读取文件夹中的多个文件 - Python/Pandas - Read multiple files in a folder based on dates in filenames Python更改文件夹中多个文件的文件名 - Python changing filenames of multiple files in a folder 使用python脚本将一个文件夹中的多个jpeg文件和json文件复制并排序到子文件夹文件夹中的另一个文件夹 - copy and sort multiple jpeg files and json files in one folder to another folder in subfolders folder using python script Python:根据文件名将文件移动到文件夹 - Python: Moving files to folder based on filenames 读取多个文件夹并将多个文本文件内容合并到每个文件夹一个文件 - Python - Read in multiple folder and combine multiple text files contents to one file per folder - Python Python 3.5:根据文件名将文件移动到文件夹 - Python 3.5: Moving files to folder based on filenames 通过使用python添加前缀来重命名文件夹中的多个文件 - Renaming multiple files in a folder by adding Prefix using python 有没有办法使用 Python 读取和写入文件夹中的多个文本文件? - Is there a way to read and write on multiple text files in a folder using Python? 如何使用for循环从python中的一个文件夹中读取多个图像? - how to read multiple images from one folder in python using for loop? 使用python从Linux中的文件夹中恢复文件名 - Recovering filenames from a folder in linux using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM