Walk through folders until specific ending number python -
i have range of folders named folder0, folder2,..., folder99. want walk through folder0,..., folderx , print files. x should stay variable , easy change.
my code looks not working how want work yet because can't decide until number want go.
import os import re rootdir = r'path' root, dirs, files in os.walk(rootdir): dir in dirs: if not re.match(r'folder[0-9]+$', dir): dirs.remove(dir) file in files: print files
assuming name scheme consistent, state, why os.walk?
import os dir_path = '/path/to/folders/folder{}' x = 10 in range(0, x): formatted_path = dir_path.format(i) try: f in os.listdir(formatted_path): filename = os.path.join(formatted_path, f) if os.path.isfile(filename): print filename except oserror: print "{} not exist".format(formatted_path)
Comments
Post a Comment