[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: RE:question
From: JYOUNG79 () kc ! rr ! com
Date: 2007-08-27 13:16:47
Message-ID: efaff46c3f029.3f029efaff46c () rdc-kc ! rr ! com
[Download RAW message or body]
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()
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic