Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Practice Makes Perfect!

For the inputs, use folders in your Mac. Try out different regular expressions as the input keyword.

Implement a stand-alone script that does the following function

input -

taking an argument “root_dir” as a root directory to start traversing taking an argument “keyword” as a regular expression for example ( “^[a-zA-Z]+_TESTResult.*” ) to detect a file contains an interested string

Functionality-

script should recursively walk the “root_dir” and detect all the files under that dir which contains “keywords” and count the number of files for each sub dir. All results should be saved in a key:value array with key being subdir string, and value being counts of files containing the keyword

Output - (1) An output array of all the data, for example {’a/b’: 6, ’a/b/c’: 7, ‘/a/b/c/d’:0} (2) An output graph with a plot with X as subdir name string, Y as count

So above is a sample quiz I pulled from the web to try to do..

To be honest Im not 100% sure on where to start Normally I break it down to be less jargon first see below

  • taking an argument “root_dir” as a root directory to start traversing

  • taking an argument “keyword” as a regular expression

  • recursively walk the “root_dir” - (This I'm not quite sure how to do)

  • detect all the files under that dir contains “keywords”

  • count the number of files for that sub dir

  • results should be saved in a key:value array with key being subdir string, and value being counts of file contains the key line

  • A output array of all the data, for example {’a/b’: 6, ’a/b/c’: 7, ‘/a/b/c/d’:0}

  • An output graph with a plot with X as subdir name string, Y as count values.

I believe I would import the OS, RE and Plot libraries first

import os
import re
 import matplotlib as plt

 def find(path)
    for root, dirs, files in os.walk(path) ---> is this correct to walk the root dir
      for names in files: 
        count = re.search([\w]* ]
          return count 

 print(count)
 plt.ylabel(count)
 plt.show()

make a function that searches the root - using os.walk(path) at the same time counting?

using RegEx's come up with something that will find a good amount of things while walking

while storing the count into another variable that shoots out an array??

and then plot the data so use matplotlib to make a graph....

Please help I'm lost

This is just for my own brain teaser.. Even some direction on good info on the task at hand would be appreciated

I can only really find things about regular expression on here in the python lang

Something tells me this is a lot simpler then I'm making it