[Tkinter-discuss] help please - cannot figure this out!
Vasilis Vlachoudis
Vasilis.Vlachoudis at cern.ch
Wed Jan 6 09:08:55 CET 2010
From a quick look, I think you are doing only one level scan. You
should use the os.walk function
if you want to scan the whole subdirectory structure
Vasilis
On 05/01/10 23:49, thicket wrote:
> Am basically using the same code - the first which uses a function
> getAnchors() fails to find a match, the second which I added to help debug
> works fine. I just cannot see why one works and the other does not....
>
> Could someone please put me out of my misery!!
> Using 2.6
>
> ########
> #!/usr/bin env python
> import os
> import fnmatch
> import re
>
> basedir="/home/mark/www/dev/ready"
> srchdirs=['/', '/static', '/static/lpi101', '/static/lpi102',
> '/static/lpi201', '/static/lpi202',
> '/static/misc','/static/images','/static/css','/static/code','/static/about']
> anchor=re.compile(r'id=":[a-zA-Z].*:[0-9]{3}-[0-9]{1,2}-[0-9]{1,2}')
> anhref=re.compile(r'href="/.*\.(html|css|js).*?"')
>
> anchors=[]
> hrefs=[]
>
> def readFile(file):
> try:
> fp=open(file, 'r')
> allLines=fp.readlines()
> fp.close
> return allLines
> except IOError, e:
> msg='Error %d: %s' % (e.args[0], e.args[1])
> return str(msg)
>
> # Anchors will only exist in .html files
> def getAnchors():
> for eachdir in srchdirs:
> aDir=basedir+eachdir
> for aFile in os.listdir(aDir):
> if fnmatch.fnmatch(aFile, '*.html'):
> contents=readFile(aFile)
> if type(contents) == '': print contents
> else:
> for line in contents:
> res=anchor.search(line)
> ### Fails to find a match - though does do in the code below
> if res != None:
>
> anchors.append(res.group()[4:])
>
>
> getAnchors()
> ## Below works - finds a match(s)
> contents=readFile('/home/mark/www/dev/ready/static/lpi101/lpi101-chapt-1-3.html')
> for line in contents:
> res=anchor.search(line)
> if res != None:
> anchors.append(res.group()[4:])
>
>
More information about the Tkinter-discuss
mailing list