0

i have a bunch of zip files in a directory and would like to get notified if one of them is missing .

Example code:

a = ['pattern1.zip', 'pattern2.zip', 'pattern3.zip']
b = []
for root,dirs,files in os.walk(some_path):
    for i in files:
        if i.endswith(('pattern1.zip', 'pattern2.zip', 'pattern3.zip')):
            b.append(i)

Output: b = ['test-pattern1.zip', 'test-pattern2.zip', 'test-pattern3.zip']

would like to match the contents of 'b' with 'a' and check if any of the zip files are missing

2 Answers2

3

I would take a different approach:

patterns = {'pattern1.zip', 'pattern2.zip', 'pattern3.zip'}
for root, dirs, files in os.walk(some_path):
    for f in files:
        for pattern in patterns:
            if f.endswith(pattern):
                patterns.remove(pattern)
                break

print('Missing patterns:', patterns)
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • 1
    Works like a charm , Thanks a lot – sourabh vs May 04 '18 at 10:45
  • for some strange reason it stopped working . If i rename the file (pattern1.zip) to 'patter.zip' , it is not able to detect the change and if the file with 'pattern1.zip' is deleted it is not able to detect that the file is missing . Any thoughts ? @Alex Hall – sourabh vs May 07 '18 at 05:32
  • Also , if there are multiple subdirectories with similar files matching the patterns ,the script is not able to iterate over them – sourabh vs May 07 '18 at 05:40
  • Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, try to figure it out, and if you get stuck, ask a new question on SO with a [mcve] that demonstrates only one of these problems as precisely as possible. But my first thought is that you have multiple files ending with the same pattern in different subdirectories, and if the program sees at least one it will mark the pattern as not missing, even if others have been removed, renamed, or aren't in some of the subdirectories. – Alex Hall May 07 '18 at 10:10
3

You can convert the lists to sets and take their difference 1:

files_that_should_be_present = ['pattern1.zip', 'pattern2.zip', 'pattern3.zip']
files_that_are_present = ['pattern1.zip', 'pattern2.zip']

files_missing = list(set(files_that_should_be_present) - set(files_that_are_present))
print(files_missing)

Outputs: ['pattern3.zip']

Simon
  • 5,464
  • 6
  • 49
  • 85