Python: find missing files from pairs

I am trying to find some missing files, but those files are in a pair.

as example, we have files like: file1_LEFT file1_RIGHT file2_LEFT file2_RIGHT file3_LEFT file4_RIGHT ...

The ideea is the name is same but they have a left\right pair. Normally we have thousands of files but somewhere there, we'll find some files without a pair. Like file99_LEFT is present but RIGHT is missing (or vice-versa for sides).

I'm trying to make a script in python 2.7 (yes i'm using an old python for personal reasons... unfortunately) but i have no clue how can be realized. ideas tried: -verify them 2 by 2 and check if we have RIGHT in current file and LEFT in previous, print ok, else print the file that's not matching. But after first one is printed, all others are failing due to fact that the structure is changed, at that point we won't have left-right one next to eachother, their order will be re-arranged -create separate lists for LEFT and RIGHT and compare them but again first one will be found but won't work for others.

Code i've used until now:

import os
import fnmatch,re



path = raw_input('Enter files path:')

for path, dirname, filenames in os.walk(path):
        for fis in filenames:
            print fis

        print len(filenames)
        for i in range(1,len(filenames),2):
            print filenames[i]
            if "RIGHT" in filenames[i] and "LEFT" in filenames[i-1]:
                print "Ok"
            else:
                print "file >"+fis+"< has no pair"
                f = open(r"D:\rec.txt", "a")
                f.writelines(fis + "\n")

f.close()

Thanks for your time!



Read more here: https://stackoverflow.com/questions/68465063/python-find-missing-files-from-pairs

Content Attribution

This content was originally published by Gogu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: