Find files, folders in your directory with Python

Richard Taujenis
Nerd For Tech
Published in
3 min readMar 11, 2021

--

Summary

Python has many modules in its standard library.To look at directory files the most popular modules are os, and global, pathlib and maybe few others I am not aware about.

Fore and foremost everyone who programs on python will come across these modules.

IG_post_project

The os module provides easy functions that allow us to interact and get Operating System information. Sys module provides access to some variables used or maintained by the interpreter and to functions.

Note: the choose_option() function is based on other project that I published and is linked to this one.So if your not interested you can skip this part.

Check Directory

Create a function that uses os.walk method we will do it in Pictures directory. So will either already choose a file in the directory or use one of the folders available there.

Find in folders

Python method walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up.

Inside the for loop when we choose 1 we choose to loop through the existing folders within that directory. Empty array is being created to store the root location and dir with os.path.join(root, dir) when its done exit the loop thus print the directories.

No worries now the depth of the loop is almost over ;)

Make a new variable in our case enter_dir that will store the folders chosen from directories array.

Now loop through the newly created variable that has all the files within that particular folder.

When walking over the dir the newly created pictures variable should append the files from second array pictures = files[2] the fact behind at least for me is this:

In my directory root location is being recived, empty braces and finally the images we actually are looking for.

And finally we choose the desired file and append the whole file location to the image empty array and cancel the loop there after coresponding else statements are being placed.

Look for images in current directory

This will be a lot more easier and straight forward because we will search just for the files in the directory(no depth luckily ❤ )

See I told you a lot more simple!

Same process applies only difference is with elif to quit by pressing ‘q’ to quit the process.Ow and if neither of 1,2 or ‘q’ is pressed prompt the user to write the right value.

Conclusion

So there you go luckily we didn’t go into infinite loop. I made this a while ago corresponding to my IG posting project. If your up for the task this code can(and actually SHOULD) be modified. Because its getting all the data/files from the dictionary. I would add a slight modification with .endswith('.jpg') when looking for a picture or any other format.Due to it being in the Pictures directory I didn’t add it. Hope you gained a lot from this and will be able to implement something for your own ideas in the future!

Link to the py script bellow:

--

--

Richard Taujenis
Nerd For Tech

Quality engineer here to bring you relevant content related to Python, JavaScript(VueJS) and so much more. Find out more 🌲 https://linktr.ee/richard_taujenis