Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, stat
- def flat_walk(root):
- """Look at a single level of the filesystem and return
- a os.walk-style tuple listing subdirectories and individual files
- in root. Also includes a realpath() on starting path
- because why the hell not.
- """
- inodes = os.listdir(root)
- dirs = []
- files = []
- for inode in inodes:
- nodestat = os.stat(os.path.join(root, inode))
- if stat.S_ISDIR(nodestat.st_mode):
- dirs.append(inode)
- else:
- files.append(inode)
- return os.path.realpath(root), dirs, files
- def build_filesystem(root):
- """Build a dictionary which contains the filesystem structure from a
- given starting path root.
- """
- dirdict = {}
- path, dirs, files = flat_walk(root)
- for subdir in dirs:
- dirdict[subdir] = build_filesystem(os.path.join(path, subdir))
- for leaf in files:
- dirdict[leaf] = os.path.join(path, leaf)
- return dirdict
Advertisement
Add Comment
Please, Sign In to add comment