Python: How Many Files in folder and Sizes

In order to check the files and file sizes, we can use the os.path module.

import os
from os.path import join
for (dirname, dirs, files) in os.walk('.'):
   for filename in files:
       if filename.endswith('.txt') :
           thefile = os.path.join(dirname,filename)
           print (os.path.getsize(thefile), thefile)

Above is the actual code to check the immediate folder (location you put the Python code in.


Let’s break this down

import os
from os.path import join

The first two lines import the OS and Join modules.

for (dirname, dirs, files) in os.walk('.'):

This code line does the walkthrough, identifying the directories, folders and files, via the os.walk. The (‘.’) tells us to review the current folder we are in.

for filename in files:

This line of code will search all files within the folder.

if filename.endswith('.txt') :

This line of code provides a check. It is checking for any file names that end in .txt. If it does end in .txt then perform the next line(s) of code.

 thefile = os.path.join(dirname,filename)
           print (os.path.getsize(thefile), thefile)

These two lines of code perform the function, based on the prior code line.

The first code joins the folder and filename and places it in the variable thefile.

The second line of code does two things.

  • Uses the value ‘thefile’ and gets the file size,
  • prints both the file size and the filename.

Let’s use the above folder as our example.

Running the job for .txt our result shows the file size as 27, and name.

   for filename in files:
       if filename.endswith('.py') :
           thefile = os.path.join(dirname,filename)
           print (os.path.getsize(thefile), thefile)

If I changed the filename search to py, and run it, the following outcome is provided.

while the Windows version show 1 kb size when viewed, the Python program provides a more detail size.


Matt Cole has high regard for knowledge share. He has a desire to share critical thinking and information. With a Masters in Information Technology and a wide array of certifications, while not working full-time, he wishes to knowledge share through providing insight, information organization, and critical thinking skills.

#KnowledgeShare | Matt Cole | #infobyMattCole

Similar Posts

  • Enhance Project Outcomes

    Enhancing project outcomes is a key objective for any project manager. The success of a project is measured not only by its ability to meet the project goals but also by how well it meets or exceeds the expectations of stakeholders. In this article, we will discuss strategies for enhancing project outcomes. Recently, my role has changed…

  • Challenge Yourself

    Being a polymath means having expertise in multiple fields, but it also means challenging yourself to continually expand your knowledge and skills. The polymath mindset involves embracing challenges and pushing yourself out of your comfort zone to explore new areas of interest. One of the benefits of challenging yourself as a polymath is that it…

  • Explore New Ideas

    One of the key attributes of a polymath and for cultivating a curious mind is the ability to explore new ideas. Exploring new ideas means being open to different perspectives, approaches, and ways of thinking, and being willing to take risks and embrace uncertainty. By exploring new ideas, polymaths can develop their creative and innovative…

  • Critical Thinking Skills

    Critical thinking skills are essential for success in both personal and professional life. They help individuals to analyze complex situations, make sound decisions, and identify effective solutions to problems. Here are some strategies for developing critical thinking skills: In Summary Developing critical thinking skills is essential for success in both personal and professional life. By…

  • Stop Compartmentalizing The Sciences

    Stop Compartmentalizing the Sciences As I explore the Zettelkasten Method, one thing continues to stand out. The relationship, connection, or association content has with each other.  What do I mean by this? As we break down the content into atomic ideas, we start seeing similarities across the differing subjects.  These similarities become compatible and give…

  • Cause of Failure

    Cause of Failure Action without planning is the cause of every failure. Alex MacKenzie Tweet Listen to the Episode There are many factoids presenting planning as the action to reduce time, increase efficiency as well as providing success. Such facts used are;  For each moment spent in planning, ten minutes of execution is saved. 10/90…