1

I'm trying to execute a directory list and make a search by filename in a TinyDb database, like below:

from tinydb import TinyDB, Query
import os, fnmatch

db = TinyDB('DB_links_filenames.json')

User = Query()

def getFieldData(campo,nome):
    results = db.search(User.file == nome )
    result = [r[campo] for r in results]
    return result


listOfFiles = os.listdir('/home/files')
pattern = "*.mp4"
for entry in listOfFiles:
    if fnmatch.fnmatch(entry, pattern):
            print(getFieldData('url',entry))

The problem is the filename goes to the function as expected, but appears to be blank in db.search..., I've tried several methods like:%nome or [nome] or (nome) in order to add the variable to search query but no sucess, the result is always the same [].

If the name of file is inserted direct to the query db.search(User.file == 'filename' ) everything works normally.

Any suggestion what could be?

Paul Mark
  • 189
  • 12
  • use `print(nome)` to see what value you have in this variable in function. Maybe you have something different then you expect. – furas Sep 11 '19 at 05:22
  • @furas, if I use print(nome) the value is correct. Tested on the function too, and received the right value too. – Paul Mark Sep 11 '19 at 05:23
  • maybe there is no result in database for this value. – furas Sep 11 '19 at 05:25
  • @furas, yes the value exists, if I type the value in the place of the variable it's find normally – Paul Mark Sep 11 '19 at 05:27
  • we can't resolve it without your data and disk to see differences in names. It can be even small difference like space in name or uppercase/lowercase name, name with and without extension. – furas Sep 11 '19 at 05:32
  • you can only check `print( nome == 'filename'` ) to see if names of disk are really the same as name which you put manually. – furas Sep 11 '19 at 05:35
  • I did this test and they are, I guess that the problem is when the var is passed to command (maybe string when needs to be object?) – Paul Mark Sep 11 '19 at 05:40

0 Answers0