1

次のコードがあります。

if inputFileName: 
    if inputFileName.lower().endswith(mediaExt):
        for word in ignoreWords:
            if word not in inputFileName.lower():
                if os.path.isfile(inputDirectory):
                    try:
                        processFile(fileAction, inputDirectory, outputDestination)
                    except Exception, e:
                        logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                        logging.exception(e)
                else:
                    try:
                        processFile(fileAction, os.path.join(inputDirectory, inputFileName), outputDestination)
                    except Exception, e:
                        logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                        logging.exception(e)

ignoreWords は、ファイル名に含めたくないいくつかの単語を含むリストです。今私の問題は、これがリスト内の x 項目に対して同じファイルをループすることです。単語を1回だけ一致させたい(または、一致が完了したらprocessFileを1回実行する)が、適切な解決策を見つけることができません

4

2 に答える 2

1

交換

for word in ignoreWords:
    if word not in inputFileName.lower():

if not any(word in inputFileName.lower() for word in ignoreWords):
于 2013-05-14T07:42:24.723 に答える
0

ファイル名をループする必要があります。filename がignoreWordsリストにない場合は、破棄します。

      if inputFileName: 
            if inputFileName.lower().endswith(mediaExt):
                for word in inputFileName.lower():
                    if word not in ignoreList:
                        if os.path.isfile(inputDirectory):
                            try:
                                processFile(fileAction, inputDirectory, outputDestination)
                            except Exception, e:
                                logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                                logging.exception(e)
                        else:
                            try:
                                processFile(fileAction, os.path.join(inputDirectory, inputFileName), outputDestination)
                            except Exception, e:
                                logging.error(loggerHeader + "There was an error when trying to process file: %s", os.path.join(inputDirectory, inputFileName))
                                logging.exception(e)
于 2013-05-14T08:29:36.837 に答える