recursive list files in a dir using Python

use the following code to list the files in a dir tree

import os
import sys
fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
    for file in files:
print fileList

# List of all the files, total count of files and folders & Total size of files.
import os
import sys

fileList = []
fileSize = 0
folderCount = 0
rootdir = sys.argv[1]

for root, subFolders, files in os.walk(rootdir):
    folderCount += len(subFolders)
    for file in files:
        f = os.path.join(root,file)
        fileSize = fileSize + os.path.getsize(f)

print("Total Size is {0} bytes".format(fileSize))
print(“Total Files “, len(fileList))
print(“Total Folders “, folderCount)

26 thoughts on “recursive list files in a dir using Python

  1. Pingback: » Blog Archive » recursive list files in a dir using Python

  2. I was looking for something that would let me search for specific strings in all the files contained within a directory. While this doesn’t fulfill my requirements, it certainly was a big help. One of those situations where its just easier to find some prefab code than to search through APIs and whatnot. Short and sweet.

  3. Hi Mayank,

    Need help with a python script which would be on similar lines of the above code….. i am new to python….

    I want a script which would recursively drill the path given as input and count no. of files and folders and also sum the file sizes and final out put should be no. of files & no. of Folders and total size of files.

    I would use this script on windows machine, i know i can get this with windows explorer however windows explorer cannot go beyond 255 character length paths, and i want the script to over comes this windows explorer limitation.

    Thanks & Regards
    Srinivas Tammali

  4. Hi Mayank,

    Thanks a ton for quick turn around and an awesome code, however its failing on deeppaths with the following error, can you please help.

    Traceback (most recent call last):
    File “C:\MyBatScripts\”, line 13, in
    fileSize = fileSize + os.path.getsize(f)
    File “C:\Python26\lib\”, line 49, in getsize
    return os.stat(filename).st_size
    WindowsError: [Error 3] The system cannot find the path specified: ‘C:\\pd_dst\\Deep_Paths_upto1000\\1\\2\\3\\4\\5\\6\\7\\8\\9\\10\\11\\12\\13\\14\\15\\16\\17\\18\\19\\20\\21\\22\\23\\24\\25\\26\\27\\28\\29\\30\\31\\32\\33\\34\\35\\36\\37\\38\\39\\40\\41\\42\\43\\44\\45\\46\\47\\48\\49\\50\\51\\52\\53\\54\\55\\56\\57\\58\\59\\60\\61\\62\\63\\64\\65\\66\\67\\68\\69\\70\\71\\72\\73\\74\\75\\76\\77\\78\\test_file.txt’

  5. thanks!
    google gave me this link as first result. why i can’t find a google+ on your website so that i can share this info with my friends?

  6. Pingback: KaewGB » Blog Archive » Counting #Source Files and #Lines of Code

  7. Hello just wanted to give you a quick heads up. The words in your article seem to
    be running off the screen in Safari. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I figured I’d post to let you know.
    The style and design look great though! Hope you get the issue solved soon.
    Many thanks

  8. What is the case when sys.argv[1] gives and IndexError: list index out of range? Do I need to change the directory or move the script to the proposed directory?

  9. Hello There. I found your blog using msn. That is a really well written
    article. I’ll make sure to bookmark it and come back to learn more of your helpful info. Thanks for the post. I’ll certainly comeback.

  10. I have noticed you don’t monetize your blog, don’t waste
    your traffic, you can earn additional cash every month because you’ve got hi quality content.
    If you want to know how to make extra bucks, search for: Mertiso’s tips best adsense alternative

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s