I'm hoping to start a python program that crawls all the folders in my operating system so that I can find all the images and then place them all into a folder (e.g an_image.png). I was wondering if that would be possible, and if so then what module should I import?
Asked
Active
Viewed 139 times
-2
-
Note that questions are expected to be about a specific problem in code you already wrote, and that questions requesting for a suggested library are explicitly off-topic (see #4 in the "some questions are still off-topic" list at https://stackoverflow.com/help/on-topic). – Charles Duffy Apr 06 '20 at 21:12
1 Answers
0
IIUC, from here:
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
files = get_filepaths('/')
img_ext = ['png']
image_files = [*filter(lambda x: x.split('.') in img_ext, files)]
You can put the extenstion you want in img_ext
and change the root. I haven't run the code but I expect it will take a very long time since you are going to pass by all your files

Bruno Mello
- 4,448
- 1
- 9
- 39
-
-
If the question boiled down to "how do I use os.walk()?", the correct approach would have been to close it as duplicate. See the bullet point regarding questions that "have been asked and answered many times before" in the *Answer Well-Asked Questions* section of [How to Answer](https://stackoverflow.com/help/how-to-answer). – Charles Duffy Apr 07 '20 at 12:51