question

JYOUNG79 at kc.rr.com JYOUNG79 at kc.rr.com
Mon Aug 27 09:16:47 EDT 2007


Hi MRAB,

Thanks so much for taking the time to help me with this!  I've learned a ton from the 
example you sent!  I 
studied each part of your code so I could get a good understanding of how it worked.  There 
was one question I 
had; on the regex line, you used "(\d+)$" and I was curious why you used the parenthesis 
around the '\d+'?

After studying the 'listdir' a bit, I realized it only grabbed the files from the current directory I 
was in.  I went 
online and with a little bit of searching I found 'walk'.  I'm not sure if I'm using this correctly, 
but I found a way 
to use 'walk' to grab all the appropriate files in nested folders too (I attached the code below).  
I'm really 
impressed with the power of Python!!

Again, thanks MRAB for the incredible help you've given me.  I really appreciate it!!  :-)

Jay

#!/usr/bin/python

import os
import re

folder = "~/Desktop/logoFiles/"

pathList = []

for root, dirs, files in os.walk(folder):
  for name in files:
    if name.upper().startswith("LOGO"):
	pathList.append(os.path.join(root, name))

dList = {}

for x in pathList:
    parts = x.split("/")
    if parts[-2].isdigit():  # Make sure folder name is a number
        n = re.findall(r"(\d+)$", parts[-1])   # Grab number off file name
        if n:
            dList[int(n[0])] = "%s<::>LOGO%s\n" % (x, n[0])

keyList = dList.keys()
keyList.sort()

sortedList = "".join([dList[x] for x in keyList])

newFile = open("/Users/jyoung1/Desktop/LogoBook_Data/sortedData.txt", "w")
newFile.write(sortedList)
newFile.close()



More information about the Python-list mailing list