How to clone a Python generator object?
Python
Consider this scenario:
#!/usr/bin/env python# -*- coding: utf-8 -*-import oswalk = os.walk('/home')for root, dirs, files in walk: for pathname in dirs+files: print os.path.join(root, pathname)for root, dirs, files in walk: for pathname in dirs+files: print os.path.join(root, pathname)
I know that this example is kinda redundant, but you should consider that we need to use the same walk data more than once. I've a benchmark scenario and the use of same walk data is mandatory to get helpful results.
I've tried walk2 = walk to clone and use in the second iteration, but it didn't work. The question is... How can I copy it? Is it ever possible?
Thank you in advance.
Answer link : https://codehunter.cc/a/python/how-to-clone-a-python-generator-object
Posted May 4, 2022, 6:38 PM UTC
10