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

  • Zeigarnik Effect

    By definition the Zeigarnik effect is the psychological tendency to remember an uncompleted task rather than a completed one or the desire to finish something that is incomplete. It nags at you, till it is done. The idea of unfinished tasks using a significant amount of your mental resources, even when you are not directly…

  • Defining the Problem

    Defining the problem is the first step in creating a successful project plan. Without a clear understanding of the problem, it’s impossible to develop a comprehensive plan that will address the root cause of the issue. In this article, we will discuss the importance of defining the problem and provide tips for effective problem definition….

  • Critical Thinking Model – Technical Support

    In today’s rapidly evolving technological landscape, the ability to solve technical support issues effectively and efficiently has become increasingly crucial. To tackle such challenges, employing a critical thinking model can significantly enhance problem-solving capabilities. Critical thinking involves a systematic approach that encompasses various steps to determine information needs, utilize diverse sources, consult experts, evaluate credibility…

  • Placed in a Box

    The term ‘placed in a box’ can also be said as ‘put in a box’ and has duel meanings. To be buried in a coffin or To be categorized as a type of person who …. Both meanings have negative connotations and harm the individual. The person placed in the coffin signifying death and the…

  • I Was a Sucker for Irrelevancy

    Many of us desire the ability to multitask, the allure of being efficient and extremely productive, not fall into irrelevancy. Many do not realize the term multitask appeared in an IBM paper describing its capabilities, 1965. Since then, the term has been transitioned to human tasks, in hopes to give the persona of efficiency. This…