6

My folder structure is as follows
Folder A
Folder B1
Folder B2
....
Folder Bn

How can I count the number of files in each of the folders (Folder B1 - Folder Bn), check if the number of files is larger than a given limit and print the folder name and number of files in it on the screen?

Like this:
Folders with too many files:
Folder B3 101
Folder B7 256

Here's what I've tried so far. It goes through every subfolder in each of my Folder B1 etc. I just need file count in one level.

import os, sys ,csv
path = '/Folder A/'

outwriter = csv.writer(open("numFiles.csv", 'w')

dir_count = []

for root, dirs, files in os.walk(path):
    for d in dirs:
        a = str(d)
        count = 0
        for fi in files:
            count += 1
        y = (a, count)
        dir_count.append(y)

    for i in dir_count:
        outwriter.writerow(i)

And then I just printed numFiles.csv. Not quite how I'd like to do it. Thanks in advance!

4
  • Try this library docs.python.org/2/library/os.html Commented Oct 30, 2014 at 11:27
  • I've updated my post to show what I've tried so far, Alex Thornton. I'm new to Python, so I really need help. Commented Oct 30, 2014 at 11:39
  • are they all in one directory? Commented Oct 30, 2014 at 11:42
  • All of Folder B are inside Folder A. Commented Oct 30, 2014 at 11:46

2 Answers 2

8

As the are all contained in that single folder, you only need to search that directory:

import os
path = '/Folder A/'
mn = 20
folders = ([name for name in os.listdir(path)
            if os.path.isdir(os.path.join(path, name)) and name.startswith("B")]) # get all directories 
for folder in folders:
    contents = os.listdir(os.path.join(path,folder)) # get list of contents
    if len(contents) > mn: # if greater than the limit, print folder and number of contents
        print(folder,len(contents)
Sign up to request clarification or add additional context in comments.

Comments

0

os.walk(path) gives you three tuple for a directory, ie (directory,subdirectory,files). directory -> list of all directory in current dir, list of subdirectory in current dir, list of files in current dir.
so you can code likes this:

import os
for dir,subdir,files in os.walk(path):
    if len(files) > your_limit:
        print dir + " has crossed limit, " + "total files: " + len(files)
        for x in files:
            print x

if you want to walk only one level, you need to code like this:

for x in os.listdir(path):
    if os.path.isdir(x):
        count = len([ y for y in os.listdir(x) if os.path.isfile(os.path.join(x,y)) ])
        if count > your_limit:
            print x + " has crossed limit: ", +count

1 Comment

This worked too, but output was nicer with Padraics' code. Thanks anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.