简体   繁体   中英

Raise os.walk when file not found

I want the the script to stop running if a "key" was not found in a file. If my directory looks like this:

Dir
   ->file.xml
   ->file2.xml
   ->yyy.m
   ->ignoreTXT.txt
   ->ignoreC.c
   ->ignoreANYTHING.anything
   ->Dir2 -> file3.xml, xxx.m, yyy.m

Lets say i have two keys in my json file: "Hello" and "World" If the key "Hello" was found in file2.xml and xxx.m it should print it out and continue with the next key. If "World" was not found it should give an Exception .

The first issue is that my script looks through other files (such as .txt , .c etc). I want to ignore them but I don't want to use os.remove since it will remove the files and I need them. Are there any other alternative? Because due to this my "raise Exception" will give error since "Hello" was not found in .txt file (which I dont care about).

The second issue is that even though "Hello" was found it will give me an error since it could not find anything in file.xml or file3.xml , ignoreTXT.txt , ignoreC.txt etc.

jdata = json.load(open(json_path))
path = findAllFiles(directory)

if os.path.isdir(path):
    for root, dirs, files in os.walk(path):
        for key, value in jdata.iteritems():
            for name in files:
                with open(os.path.join(root, name)) as fle:
                    content = fle.read()
                if name.endswith('.txt') and re.search(Wordboundry(key), content):
                    print "Name", key, "was found in", name, "\n"
                    writeToFile(value, content, name, key)
               else:
                   print "Name", key, "was not found in", name
    else:
        raise Exception(key, "was not found in", root) #This gives me error even though "Hello" can be found in file2.xml. 



def FindName(content, key, name, value):
    if name.endswith('.xml') and re.search(Wordboundry(key), content):
        print "Name", key, "was found in", name, "\n"
        OverrideXML(key, value, name)
    elifif name.endswith('.m') and re.search(Wordboundry(key), content):
        print "Name", key, "was found in", name, "\n"
        OverrideM(key, value, name)

You can pretty easily filter out non .xml files as follows:

if os.path.isdir(path):
    for root, dirs, files in os.walk(path):

        xmlFiles = [file for file in files if file.endswith(".xml")]

        for key, value in jdata.iteritems():
            for name in xmlFiles:
                ...

This creates a sublist of files which only contains those names ending in the xml file extension. You can then loop through this sublist for your search

You put your else in the wrong indentation. This will work:

if os.path.isdir(path):
    for root, dirs, files in os.walk(path):
        for key, value in jdata.iteritems():
            for name in files:
                if not name.endswith('.txt'):
                    continue
                with open(os.path.join(root, name)) as fle:
                    content = fle.read()
                if re.search(Wordboundry(key), content):
                    print "Name", key, "was found in", name, "\n"
                    writeToFile(value, content, name, key)
                else:
                    print "Name", key, "was not found in", name
                    raise Exception(key, "was not found in", root)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM