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:
fileList.append(os.path.join(root,file))
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(f)
fileList.append(f)
print("Total Size is {0} bytes".format(fileSize))
print(“Total Files “, len(fileList))
print(“Total Folders “, folderCount)
Pingback: ownport.net » Blog Archive » recursive list files in a dir using Python
Hey, this is simple but perfect..!
This is just what I was looking for! So simple and it works like a charm.. thanks!
this is not understadable. I want python programms only.
not sure what you mean by that. can you inform me in details
Was searching for this recipe and yours was the first result, perfect! Thanks for posting it.
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.
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
Check out the second script, i think it should do the job,
enjoy,
mayank
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\new.py”, line 13, in
fileSize = fileSize + os.path.getsize(f)
File “C:\Python26\lib\genericpath.py”, 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’
Mayank,
Also just noticed that the script is returning files = 1 always.
It seems like windows issue
. I was able to get the details for folder which have less than 255 folder path length. but errors out for others
.
Yeah, i need it for paths longer than 255 char length
Thank you very much for sharing your code!
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?
I didn’t have installed the Google+ plugin for WordPress, I think.
I = he