user974873
user974873

Reputation: 321

How can I scan through a directory in python?

I have a python script that is trying to compare two files to each other and output the difference. However I am not sure what exactly is going on as when I run the script it gives me an error as

NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\api\\API_TEST\\Apis.os\\*.*'

I dont know why it is appending * . * at the end of the file extention.

This is currently my function:

def CheckFilesLatest(self, previous_path, latest_path):

    for filename in os.listdir(latest_path):

        previous_filename = os.path.join(previous_path, filename)
        latest_filename = os.path.join(latest_path, filename)

        if self.IsValidOspace(latest_filename):

            for os_filename in os.listdir(latest_filename):
                name, ext = os.path.splitext(os_filename)

                if ext == ".os":
                    previous_os_filename = os.path.join(previous_filename, os_filename)
                    latest_os_filename = os.path.join(latest_filename, os_filename)

                    if os.path.isfile(latest_os_filename) == True:

                        # If the file exists in both directories, check if the files are different; otherwise mark the contents of the latest file as added.
                        if os.path.isfile(previous_os_filename) == True:
                            self.GetFeaturesModified(previous_os_filename, latest_os_filename)
                        else:
                            self.GetFeaturesAdded(latest_os_filename)

        else:
            if os.path.isdir(latest_filename):
                self.CheckFilesLatest(previous_filename, latest_filename)

Any thoughts on why it cant scan the directory and look for an os file for example?

It is failing on line:

for os_filename in os.listdir(latest_filename):

The code first gets called from

def main():
    for i in range(6, arg_length, 2):
        component = sys.argv[i]
        package = sys.argv[i+1]

        previous_source_dir = os.path.join(previous_path, component, package)
        latest_source_dir = os.path.join(latest_path, component, package)

        x.CheckFilesLatest(previous_source_dir, latest_source_dir)
        x.CheckFilesPrevious(previous_source_dir, latest_source_dir)

Thank you

Upvotes: 0

Views: 804

Answers (2)

sabbahillel
sabbahillel

Reputation: 4425

os.listdir() requires that the latest_path argument be a directory as you have stated. However, latest_path is being passed in as an argument. Thus, you need to look at the code that actually creates latest_path in order to determine why the '.' is being put in. Since you are calling it recursively, first check the original call (the first time). It would appear that your base code that calls CheckFilesLatest() is trying to set up the search command to find all files within the directory 'C:\api\API_TEST\Apis.os' You would need to split out the file indicator first and then do the check.

Upvotes: 1

Max Noel
Max Noel

Reputation: 8910

If you want to browse a directory recursively, using os.walk would be better and simpler than your complex handling with recursive function calls. Take a look at the docs: http://docs.python.org/2/library/os.html#os.walk

Upvotes: 0

Related Questions