Search code examples
python-3.xglob

Trying to use boolean comparison for file search


I'm trying to code a Python script to find files in a directory that contain two keywords in its file contents. I have posted a question before that referred to a basic issue with a much simpler version of this code, but I wasn't sure if I needed to post this separately since I am now looking at a different issue.

import glob
import os
import sqlite3
import re

conn = sqlite3.connect( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\YGOPro-Support-System\\http\\ygopro\\databases\\0-en-OCGTCG.cdb" )
curs = conn.cursor()

#Define string constants
trig = "EFFECT_TYPE_TRIGGER"
summon = "SUMMON_SUCCESS"
flip = "EFFECT_TYPE_FLIP"
flip2 = "EVENT_FLIP"
pos = "EVENT_CHANGE_POS"
spelltrap = "EFFECT_TYPE_ACTIVATE"
banish = "EVENT_REMOVE"
grave = "EVENT_TO_GRAVE"

os.chdir( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\Salvation-Scripts-TCG" )
for files in glob.glob( "*.lua" ) :
    f = open( files, 'r', encoding = "iso-8859-1" )
    for line in f :
        files = re.sub('[c.luatilityold]', '', files)
        #Use database to print names corresponding to each file ID for verification purpose
        result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (files,))
        x = result.fetchone()
        #Check for files that have both 'trig' and 'banish' values in contents
        if trig and banish in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'grave' values in contents
        elif  trig and grave in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'summon' values in contents
        elif trig and summon in line :
            if x is not None :
                print ( x )
        #Check for files that have 'flip' value in contents
        elif flip in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'flip2' values in contents
        elif trig and flip2 in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'pos' values in contents
        elif trig and pos in line :
            if x is not None :
                print ( x )
        #Ignore other files
        else :
            pass

The issue that I'm having is that the if-cases aren't working properly. The trig variable gets ignored while the code is running, and it therefore only looks at the second key. I have tried using wording such as if trig in line and banish in line, but the problem is that it will only look for files that have these two keys together on the same line of a file's contents. I need this to be able to find a file that has the two keys anywhere in the file. Is there a better way to search for the two keys in one go like how I was trying to do, or is there a different approach that I need to take?


Solution

  • Since this code relies on databases specific to your machine, I cannot test whether my changes work to create your expected outputs.

    I am not really sure what you are trying to do here. If you want to search for the keywords in the whole file, read in the entire file at once instead of line by line.

    import glob
    import os
    import sqlite3
    import re
    
    conn = sqlite3.connect( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\YGOPro-Support-System\\http\\ygopro\\databases\\0-en-OCGTCG.cdb" )
    curs = conn.cursor()
    
    #Define string constants
    trig = "EFFECT_TYPE_TRIGGER"
    summon = "SUMMON_SUCCESS"
    flip = "EFFECT_TYPE_FLIP"
    flip2 = "EVENT_FLIP"
    pos = "EVENT_CHANGE_POS"
    spelltrap = "EFFECT_TYPE_ACTIVATE"
    banish = "EVENT_REMOVE"
    grave = "EVENT_TO_GRAVE"
    
    os.chdir( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\Salvation-Scripts-TCG" )
    for filename in glob.glob( "*.lua" ) :
        with open(filename, 'r', encoding = "iso-8859-1") as content_file:
            content = content_file.read()
            query_file = re.sub('[c.luatilityold]', '', filename)
            #Use database to print names corresponding to each file ID for verification purpose
            result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (query_file ,))
            x = result.fetchone()
            #Check for files that have both 'trig' and 'banish' values in contents
            if trig in content and banish in content:
                if x is not None :
                    print ( x )
            #Check for files that have both 'trig' and 'grave' values in contents
            elif  trig in content and grave in content:
                if x is not None :
                    print ( x )
            ...
    

    You definitely want to use the syntax

    if x in content and y in content
    

    Otherwise, the conditional will always be evaluated to True because the string is always non-empty and will therefore be always True (as indicated in a comment in your last post).

    I assumed in your code that the reuse of the variable files in

    files = re.sub('[c.luatilityold]', '', files)
        #Use database to print names corresponding to each file ID for verification purpose
        result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (files,))
        x = result.fetchone()
    

    is incidental. Changing the value stored in files should not affect anything else in this instance, but if you try to get the file path of the current file, you will not be getting what you expected. As such, I would also recommend using a different variable for this operation like I did.