472,958 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Matching Directory Names and Grouping Them

J
Hello Group-

I have limited programming experience, but I'm looking for a generic
way to search through a root directory for subdirectories with similar
names, organize and group them by matching their subdirectory path, and
then output their full paths into a text file. For example, the
contents of the output text file may look like this:

<root>\Input1\2001\01\
<root>\Input2\2001\01\
<root>\Input3\2001\01\

<root>\Input1\2002\03\
<root>\Input2\2002\03\
<root>\Input3\2002\03\

<root>\Input2\2005\05\
<root>\Input3\2005\05\

<root>\Input1\2005\12\
<root>\Input3\2005\12\

I tried working with python regular expressions, but so far haven't
found code that can do the trick. Any help would be greatly
appreciated. Thanks!
J.

Jan 11 '07 #1
4 1192
J wrote:
Hello Group-

I have limited programming experience, but I'm looking for a generic
way to search through a root directory for subdirectories with similar
names, organize and group them by matching their subdirectory path, and
then output their full paths into a text file. For example, the
contents of the output text file may look like this:

<root>\Input1\2001\01\
<root>\Input2\2001\01\
<root>\Input3\2001\01\

<root>\Input1\2002\03\
<root>\Input2\2002\03\
<root>\Input3\2002\03\

<root>\Input2\2005\05\
<root>\Input3\2005\05\

<root>\Input1\2005\12\
<root>\Input3\2005\12\

I tried working with python regular expressions, but so far haven't
found code that can do the trick. Any help would be greatly
appreciated. Thanks!
Define "similar".

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com

Jan 11 '07 #2
J
Steve-

Thanks for the reply. I think what I'm trying to say by similar is
pattern matching. Essentially, walking through a directory tree
starting at a specified root folder, and returning a list of all
folders that matches a pattern, in this case, a folder name containing
a four digit number representing year and a subdirectory name
containing a two digit number representing a month. The matches are
grouped together and written into a text file. I hope this helps.

Kind Regards,
J

Steve Holden wrote:
J wrote:
Hello Group-

I have limited programming experience, but I'm looking for a generic
way to search through a root directory for subdirectories with similar
names, organize and group them by matching their subdirectory path, and
then output their full paths into a text file. For example, the
contents of the output text file may look like this:

<root>\Input1\2001\01\
<root>\Input2\2001\01\
<root>\Input3\2001\01\

<root>\Input1\2002\03\
<root>\Input2\2002\03\
<root>\Input3\2002\03\

<root>\Input2\2005\05\
<root>\Input3\2005\05\

<root>\Input1\2005\12\
<root>\Input3\2005\12\

I tried working with python regular expressions, but so far haven't
found code that can do the trick. Any help would be greatly
appreciated. Thanks!
Define "similar".

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
Jan 11 '07 #3
>From your example, if you want to group every path that has the same
last 9 characters, a simple solution could be something like:

groups = {}
for path in paths:
group = groups.setdefault(path[-9:],[])
group.append(path)

I didn't actually test it, there ight be syntax errors.

J wrote:
Steve-

Thanks for the reply. I think what I'm trying to say by similar is
pattern matching. Essentially, walking through a directory tree
starting at a specified root folder, and returning a list of all
folders that matches a pattern, in this case, a folder name containing
a four digit number representing year and a subdirectory name
containing a two digit number representing a month. The matches are
grouped together and written into a text file. I hope this helps.

Kind Regards,
J

Steve Holden wrote:
J wrote:
Hello Group-
>
I have limited programming experience, but I'm looking for a generic
way to search through a root directory for subdirectories with similar
names, organize and group them by matching their subdirectory path, and
then output their full paths into a text file. For example, the
contents of the output text file may look like this:
>
<root>\Input1\2001\01\
<root>\Input2\2001\01\
<root>\Input3\2001\01\
>
<root>\Input1\2002\03\
<root>\Input2\2002\03\
<root>\Input3\2002\03\
>
<root>\Input2\2005\05\
<root>\Input3\2005\05\
>
<root>\Input1\2005\12\
<root>\Input3\2005\12\
>
I tried working with python regular expressions, but so far haven't
found code that can do the trick. Any help would be greatly
appreciated. Thanks!
>
Define "similar".

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
Jan 12 '07 #4
On 2007-01-11, J <wi***********@gmail.comwrote:
Steve-

Thanks for the reply. I think what I'm trying to say by similar
is pattern matching. Essentially, walking through a directory
tree starting at a specified root folder, and returning a list
of all folders that matches a pattern, in this case, a folder
name containing a four digit number representing year and a
subdirectory name containing a two digit number representing a
month. The matches are grouped together and written into a text
file. I hope this helps.
Here's a solution using itertools.groupby, just because this is
the first programming problem I've seen that seemed to call for
it. Hooray!

from itertools import groupby

def print_by_date(dirs):
r""" Group a directory list according to date codes.
>>data = [
... "<root>/Input2/2002/03/",
... "<root>/Input1/2001/01/",
... "<root>/Input3/2005/05/",
... "<root>/Input3/2001/01/",
... "<root>/Input1/2002/03/",
... "<root>/Input3/2005/12/",
... "<root>/Input2/2001/01/",
... "<root>/Input3/2002/03/",
... "<root>/Input2/2005/05/",
... "<root>/Input1/2005/12/"]
>>print_by_date(data)
<root>/Input1/2001/01/
<root>/Input2/2001/01/
<root>/Input3/2001/01/
<BLANKLINE>
<root>/Input1/2002/03/
<root>/Input2/2002/03/
<root>/Input3/2002/03/
<BLANKLINE>
<root>/Input2/2005/05/
<root>/Input3/2005/05/
<BLANKLINE>
<root>/Input1/2005/12/
<root>/Input3/2005/12/
<BLANKLINE>

"""
def date_key(path):
return path[-7:]
groups =[list(g) for _,g in groupby(sorted(dirs, key=date_key), date_key)]
for g in groups:
print '\n'.join(path for path in sorted(g))
print

if __name__ == "__main__":
import doctest
doctest.testmod()

I really wanted nested join calls for the output, to suppress
that trailing blank line, but I kept getting confused and
couldn't sort it out.

It would better to use the os.path module, but I couldn't find
the function in there lets me pull out path tails.

I didn't filter out stuff that didn't match the date path
convention you used.

--
Neil Cerutti
Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which I guess will speed it up in the end. 2.) This...
2
by: Jim Dabell | last post by:
I'm in the middle of writing a small app for Linux that needs to create directories that take their names from untrusted data. If possible, I'd like to preserve special characters rather than...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
2
by: NeverLift | last post by:
I can't believe I'm the first to encounter this, but I've not found any similar posting. Been developing pages for a few months, getting up to speed in javascript tricks, inline incremental...
2
by: Sheila | last post by:
I am hoping someone here has experience with this. I am using Access 97, but if someone has done this in another version I'd appreciate input as well I need to populate a table with subdirectory...
1
by: Trint Smith | last post by:
I can get file names with the following code... Dim di As New DirectoryInfo("c:\OrderData\Database") Dim fiArr As FileInfo() = di.GetFiles() Dim f As FileInfo For Each f In fiArr Dim...
2
by: Steven J. Reed | last post by:
I have a Web Service that needs to find a Virtual Directory name, determine the actual path of that directory, then read / write files to that directory. How can I find a virtual directory then...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
5
by: Martin Carpella | last post by:
Hi! I know, that Path-names should be shorter than MAX_PATH (250), but the Win32-SDK allows specifiying longer names using the "\\?\" prefix. As I have an application that accesses files...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.