HI all,
I am trying to write a while loop that will iterate over generators to
capture all the headers of FFCache directories. However, the
generators embedded within the argument of another generator do not
seem to re-initiate. the example below loops through and initiates the
generator embedded in the argument only once. Can anyone explain while
the generator will not re-initiate, and suggest a simple fix?
#!/usr/bin/env python
import os,struct,time
def generate_header s(cachedir):
for name, blocksize in cachefiles:
pathname = os.path.join(ca chedir,name)
f = open(pathname," rb")
f.seek(4096)
while True:
header = f.read(36)
if not header: break
fields = struct.unpack(" >9I",header)
if fields[0] == 0x00010008: yield f, fields
fp = f.tell()
offset = fp % blocksize
if offset:
f.seek(blocksiz e - offset,1)
f.close()
def concatenate(seq uences):
for seq in sequences:
for item in seq:
yield item
all_caches = (path for path,dirlist,fi lelist in os.walk("/Users/") if
'_CACHE_MAP_' in filelist)
cachefiles = [('_CACHE_001_', 256),('_CACHE_0 02_',1024),('_C ACHE_003_',
4096)]
n = 0
while True:
n += 1
time.sleep(0.5)
headers = concatenate(gen erate_headers(p ath) for path in
all_caches)
for h in headers:
print h,n
# this doesn't work either
while True:
n += 1
time.sleep(0.5)
for path in all_caches:
headers = generate_header s(path)
for h in headers:
print h,n
# however if I hard code the path
path = "FFCache"
while True:
n += 1
headers = generate_header s(path)
for h in headers:
print h,n
#but of course i do not wish to hard code the path.
I am trying to write a while loop that will iterate over generators to
capture all the headers of FFCache directories. However, the
generators embedded within the argument of another generator do not
seem to re-initiate. the example below loops through and initiates the
generator embedded in the argument only once. Can anyone explain while
the generator will not re-initiate, and suggest a simple fix?
#!/usr/bin/env python
import os,struct,time
def generate_header s(cachedir):
for name, blocksize in cachefiles:
pathname = os.path.join(ca chedir,name)
f = open(pathname," rb")
f.seek(4096)
while True:
header = f.read(36)
if not header: break
fields = struct.unpack(" >9I",header)
if fields[0] == 0x00010008: yield f, fields
fp = f.tell()
offset = fp % blocksize
if offset:
f.seek(blocksiz e - offset,1)
f.close()
def concatenate(seq uences):
for seq in sequences:
for item in seq:
yield item
all_caches = (path for path,dirlist,fi lelist in os.walk("/Users/") if
'_CACHE_MAP_' in filelist)
cachefiles = [('_CACHE_001_', 256),('_CACHE_0 02_',1024),('_C ACHE_003_',
4096)]
n = 0
while True:
n += 1
time.sleep(0.5)
headers = concatenate(gen erate_headers(p ath) for path in
all_caches)
for h in headers:
print h,n
# this doesn't work either
while True:
n += 1
time.sleep(0.5)
for path in all_caches:
headers = generate_header s(path)
for h in headers:
print h,n
# however if I hard code the path
path = "FFCache"
while True:
n += 1
headers = generate_header s(path)
for h in headers:
print h,n
#but of course i do not wish to hard code the path.
Comment