-3

I am working on file integrity check and I want to check in a given directory if the file has it's corresponding MD5 hash file and return all file names which have the corresponding md5 hash.

for example:

inside directory [
 abc.bin    abc.bin.md5
 efg.bin  
 qwerty.bin  qwerty.bin.md5
 xyc.bin  
]

the return values: abc.bin, qwerty.bin

wovano
  • 4,543
  • 5
  • 22
  • 49
  • 2
    Is there a question to be asked? I’m afraid you’ve forgotten to 1) post your coding attempt, and 2) ask a *specific* question relating to an issue in your code. – S3DEV Nov 29 '22 at 19:50
  • Its easier than you're making it out to be – stefan_aus_hannover Nov 29 '22 at 19:52
  • 2
    If you know the filename is `abc.bin`, then just look for a file named `abc.bin.md5`. What is the difficulty? – John Gordon Nov 29 '22 at 20:04
  • Just use the standard `md5sum` tool: `md5sum -c abc.bin.md5`. In Windows you could use `for %f in (*.md5) do md5sum -c %f` and in Unix something similar. See https://man7.org/linux/man-pages/man1/md5sum.1.html – wovano Nov 29 '22 at 20:32
  • 1
    Start here: https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory – Thomas Weller Nov 29 '22 at 20:43
  • @wovano I don't think the question even needs md5 hashing functionality. The request is basically "Given a list of filenames, return matches where a match is between a filename and the same file with `.md5` appended to it". (If I'm understanding correctly. – JNevill Nov 29 '22 at 20:43
  • @JNevill Yes thats exactly I was looking for can you please help me with it. – stack overflow Nov 30 '22 at 00:50
  • What have you tried so far? Where are you stuck? Do you know how to do a directory listing? Do you know how to do string manipulation? Do you know how to check if a file exists? These are 3 very basic things you'll quickly learn in any tutorial (and for which there are plenty examples on Stack Overflow). Combining these things in a few lines of code will al be that's necessary to implement your task. I don't really know what we should explain here. Please describe the **specific** problem you're stuck with. – wovano Nov 30 '22 at 07:06

1 Answers1

0

You could do something like this:

#get a list of files and directories in the current directory
file_list = os.listdir('.')

#produce a list of candidate files (files ending with `md5`). Perhaps there is a more elegant way of doing this, but it works. 
candidates = [x for x in file_list if x.split('.')[-1] == 'md5']

#iterate the file_list and check to see if there is a match in candidates (after removing the `.md5` portion of the name). 
print([file for file in file_list if file in ['.'.join(candidate.split('.')[:-1]) for candidate in candidates]] + candidates)

There are a million ways to go about doing this so feel free to tinker.

JNevill
  • 46,980
  • 4
  • 38
  • 63