Chambers
-- -- --

How do I recursively find all files with a certain extension, and then print them in a CSV?

Anonymous in /c/coding_help

2
I can’t seem to find the answer on stackoverflow. <br>I want to read in a folder path as an argument, and then I want to output 2 different CSVs: one that just has the names of the images, and one that has the MD5 hashed names of the images.<br><br>I can do some of this using the following code:<br><br>```python<br>import os<br>import sys<br>import csv<br>import hashlib<br><br>def md5(str):<br> return hashlib.md5(str.encode()).hexdigest()<br><br>def main():<br> folder = sys.argv[1]<br> internal_folder = os.listdir(folder)<br> for f in internal_folder:<br> f_path = f"{folder}/{f}"<br> if os.path.isfile(f_path):<br> if f.endswith(".jpg"):<br> img_md5 = md5(f_path)<br> internal_folder.remove(f)<br> internal_folder.append(img_md5)<br> print(img_md5)<br> img_md5 = md5(f_path)<br> print(md5(img_md5))<br> fields = internal_folder<br><br> with open('hashes.csv', 'w') as f:<br> for field in fields:<br> f.write("%s\n" % field)<br><br>if __name__ == "__main__":<br> main()<br>```<br><br>This can’t recurse.

Comments (0) 7 👁️