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

3
  • if any(i.endswith(x) for x in a) Commented May 4, 2018 at 9:42
  • 1
    Make sets of a and b and look for the set difference. Commented May 4, 2018 at 9:43
  • is it always in the format test-patternX.zip? Commented May 4, 2018 at 9:52

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Works like a charm , Thanks a lot
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
Also , if there are multiple subdirectories with similar files matching the patterns ,the script is not able to iterate over them
Read 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 minimal reproducible example 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.
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']

1 Comment

The filenames don't match exactly, it's based on endswith.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.