How to compare a premade list of files with os.walk generated list

41 views Asked by At

I am attempting to create an automated verification code for folders and files. There are certain files and folders that are required to be in a project to make sure it functions correctly. My code is supposed to walk through the folder and check if the files are there, and if they are not it would print out 'Required file {file} is missing at {location}'

The folder structure would go as:
| main folder #this can be named anything, just where I would dump the folder that required to be checked.
->| project folder #the folder that is going to be checked
-->| vocab subfolder
--->| custom.xsd
--->| strict.xsd
--->| loose.xsd
-->| unique subfolder
--->| strict.xsd
--->| loose.xsd
-->| extended subfolder
--->| strict.xsd
-->| datatypes.dtd
-->| xml.xsd
-->| anyElement.xsd
-->| dataTypes.dtd
---| . . . rest of files/folders, these are not required to exist and can be anything, but don't need to be checked. 

My code:

import os

required_files = [
    'datatypes.dtd'           ,
    'lom.xsd'                 ,
    'xml.xsd'                 ,
    'XMLSchema.dtd'           ,
    'common\\anyElement.xsd'          ,
    'common\dataTypes.xsd'           ,
    'extend\strict.xsd'       ,
    'unique\loose.xsd'        ,
    'unique\strict.xsd'       ,
    'vocab\custom.xsd'        ,
    'vocab\loose.xsd'         ,
    'vocab/strict.xsd'        , #this is purposefully made wrong. It is suppose to be my testcase to make sure my code is working as intended when this is flagged as 'missing'
]

def check_name(x):
        # this function is called in another script, where the user would click a button to 
        # choose which file to use, and that path is stored in this 'x'
        # For my testing purposes, I am using a folder on my desktop

        path = x
        directory = path.replace("/", "\\")

        for d in next(os.walk(directory))[1]:
                itempath = os.path.join(directory, d)
                path = d
                if any(letter.isupper() for letter in path):
                        print(" Root folder contains invalid characters at:\n" + itempath) # skeleton veriifcation to make sure project root folder contains no uppercase letters

                check_required_files = [] # this is my orginal idea for generating the list. 

                for i in required_files: #creates a slightly sloppy method to ensure the required files are present and verifiable
                        filepath = os.path.join(directory,d, i)
                        print(filepath)
                        check_required_files.append(filepath)

                for root, directories, files in os.walk(directory):

                        for name in directories: 
                                # Search for directories that contain any illegal characters 
                                if any(letter.isupper() for letter in name):
                                        print(f"The Folder '{name}' contains illegal characters at\n" + os.path.join(root, name))
                                        print('dirs ' + root)
                                else:
                                        pass

                        for fname in files:

                                fname = os.path.join(root, fname)
                                if any([i in fname for i in check_required_files]):
                                        print(fname)

My Problem: I've tried multiple different ways to get my premade list(It is trimmed down for ease of debugging) to be able to get to become compared with a generated list. One of the biggest hurdles I had was some files were in subfolders, but the generated list wouldn't include that, so would pop out a file that was missing but wouldn't point to where it was supposed to be. I found a sloppy way of fixing that by including the path to my premade list, and to the generated one.

The if any([i in fname for i in check_required_files]): is heading in the right direction but I can't quite get this to work, I tried doing the inverse with if not any but this would spit out the entire files. Doing if any([i in fname for i not in check_required_files]): would create a syntax error. Am I going about this the right way?

I thought about using glob, but from my (limited) searches I would need to use multiple globs to search throughout the entire folder to do for each check, and I figure that would make the code cumbersome.

0

There are 0 answers