'''Return the length of the longest string in s1.'''
ret = 0
for s in s1:
+ ss = os.path.basename(s)
if len(s) > ret:
ret = len(s)
return ret
+def list_maybe_dir(path, depth):
+ try:
+ for fname in os.listdir(path):
+ yield os.path.join(path, fname)
+ except NotADirectoryError as e:
+ if depth == 1:
+ yield path
+ return
+ raise e
+
def walk_tree(path, depth):
'''Walk the directory tree rooted by path.'''
dirs = []
files = []
- for fname in os.listdir(path):
- fullpath = os.path.join(path, fname)
+ for fullpath in list_maybe_dir(path, depth):
sb = os.lstat(fullpath)
if stat.S_ISDIR(sb.st_mode):
- dirs.append(fname)
+ dirs.append(fullpath)
continue
elif stat.S_ISSOCK(sb.st_mode):
continue
else:
- files.append(fname)
+ files.append(fullpath)
- for fname in files:
- if ' ' in fname:
+ for fullpath in files:
+ if ' ' in os.path.basename(fullpath):
msg = _("Spaces not allowed in file names.")
- raise ValueError(f'{fname}: {msg}')
- for fname in dirs:
- if ' ' in fname:
+ raise ValueError(f'{fullpath}: {msg}')
+ for fullpath in dirs:
+ if ' ' in os.path.basename(fullpath):
msg = _("Spaces not allowed in subdirectory names.")
- raise Exception(f'{fname}: {msg}')
+ raise Exception(f'{fullpath}: {msg}')
fname_width = max_fname_len(files)
- for fname in files:
- fullpath = os.path.join(path, fname)
+ for fullpath in files:
+ fname = os.path.basename(fullpath)
sb = os.lstat(fullpath)
extra = stat_to_extra(sb, fullpath)
print('%*s%-*s %s%s' % (depth, ' ', fname_width, fname, \
stat_to_str(sb), extra))
- for fname in dirs:
- fullpath = os.path.join(path, fname)
+ for fullpath in dirs:
+ fname = os.path.basename(fullpath)
sb = os.lstat(fullpath)
extra = stat_to_extra(sb, fullpath)
print('%*s%s %s' % (depth, ' ', fname, \
# Copy the first argument's stat to the rootdir
statbuf = os.stat(args.paths[0])
if not stat.S_ISDIR(statbuf.st_mode):
- raise NotADirectoryError(path)
+ raise NotADirectoryError(args.paths[0])
print(stat_to_str(statbuf))
# All files under each path go in the root dir, recursively