Pattern Matching Given # of Characters and no String Input; use RegularExpressions?

Synonymous sm.synonymous at gmail.com
Fri Apr 22 03:56:46 EDT 2005


Hello!

I was trying to create a program to search for the largest common
subsetstring among filenames in a directory, them move the filenames
to the substring's name.  I have succeeded, with help, in doing so and
here is the code.

Thanks for your help!

--- Code ---

#This program was created with feed back from: smeghead and sirup plus
aum of I2P; and also tiissa and John Machin of comp.lang.python
#Thank you very much.
#I still get the odd error in this, but it was 1 out of 2500 files
successfully sorted.  Make sure you have a directory under c:/test/
called 'aa' and have your
#I release this code into the public domain :o), send feed back to
sm.synonymous at gmail.com
files in c:/test/
import pickle
import os
import shutil
os.chdir ( '/test')
aaaa=2
aa='aa'
x=0
y=20
while y <> 2:
    print y
    List = []
    for fileName in os.listdir ( '/test/' ):
        Directory = fileName
        List.append(Directory)
    List.append("A111111111111")
    List.sort()
    List.append("Z111111111111")
    ListLength = len(List) - 1
    x = 0
    while x < ListLength:
        ListLength = len(List) - 1
        b = List[x]
        c = List[x + 1]
        backward1 = List[x - 1]
        d = b[:y]
        e = c[:y]
        backward2 = backward1[:y]
        f = str(d)
        g = str(e)
        backward3 = str(backward2)
        if f==g:
            if os.path.isdir (aa+"/"+f) == True:
                shutil.move(b,aa+"/"+f)
            else:
                os.mkdir(aa+"/"+f)
                #os.mkdir(f)
                shutil.move(b,aa+"/"+f)
        else:
            if f==backward3:
                if os.path.isdir (aa+"/"+f) == True:
                    shutil.move(b,aa+"/"+f)
                else:
                    os.mkdir(aa+"/"+f)
                    #os.mkdir(f)
                    shutil.move(b,aa+"/"+f)
            else:
                aaaa=3
        x = x + 1
    y = y - 1

--- End Code ---

sm.synonymous at gmail.com (Synonymous) wrote in message news:<ae0b1ff7.0504170134.66a01b0c at posting.google.com>...
> Hello,
> 
> Can regular expressions compare file names to one another. It seems RE
> can only compare with input i give it, while I want it to compare
> amongst itself and give me matches if the first x characters are
> similiar.
> 
> For example:
> 
> cccat
> cccap
> cccan
> dddfa
> dddfg
> dddfz
> 
> Would result in the 'ddd' and the 'ccc' being grouped together if I
> specified it to look for a match of the first 3 characters.
> 
> What I am trying to do is build a script that will automatically
> create directories based on duplicates like this starting with say 10
> characters, and going down to 1.  This way "Vacation1.jpg,
> Vacation2.jpg" would be sent to its own directory (if i specifiy the
> first 8 characters being similiar) and "Cat1.jpg, Cat2.jpg" would
> (with 3) as well.
> 
> Thanks for your help and interest!
> 
> S M



More information about the Python-list mailing list