avatarRichard Taujenis

Summary

The website content provides a tutorial on using Python's standard library modules such as os, sys, and pathlib to navigate and manipulate files and directories, with a focus on finding files and folders in a directory, specifically for an Instagram post project.

Abstract

The article titled "Find files, folders in your directory with Python" is a guide aimed at Python programmers who need to interact with the file system. It introduces the os and sys modules, which are essential for retrieving operating system information and accessing interpreter-related variables and functions. The author, Mozes721, explains how to use the os.walk() method to traverse a directory tree and lists functions to find files and folders, particularly images. The tutorial also includes code snippets hosted on GitHub Gist for practical demonstration. These snippets illustrate how to search for images within a directory, select a file, and exit the loop. The article concludes with a reflection on the code's efficiency and suggests improvements, such as filtering for specific file types using .endswith(). The author encourages readers to modify and implement the code in their projects, providing a link to the complete Python script on GitHub.

Opinions

  • The author believes that familiarity with the os and sys modules is crucial for Python programmers dealing with file system operations.
  • Mozes721 suggests that the provided code should be modified and improved by the users for their specific needs, such as filtering images by file extension.
  • The author implies that the code examples are part of a larger project related to posting images on Instagram, indicating the practical application of the tutorial.
  • There is an acknowledgment that the choose_option() function is linked to another project, and it may be skipped if not of interest to the reader.
  • The article conveys a tone of encouragement and a willingness to share knowledge, hoping that readers will benefit from the tutorial and apply it to their future endeavors.

Find files, folders in your directory with Python

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:

Python
Dictionary
Loop
Files
Directory
Recommended from ReadMedium