|
1 |
| -# Directory Tree Generator |
2 |
| - |
3 | 1 | import os
|
4 | 2 | import argparse
|
| 3 | +from datetime import datetime |
5 | 4 |
|
6 | 5 |
|
7 | 6 | def realname(path, root=None):
|
| 7 | + """Return the real name of a path, including symbolic links.""" |
8 | 8 | if root is not None:
|
9 | 9 | path = os.path.join(root, path)
|
10 | 10 | result = os.path.basename(path)
|
11 | 11 | if os.path.islink(path):
|
12 | 12 | realpath = os.readlink(path)
|
13 |
| - result = '%s -> %s' % (os.path.basename(path), realpath) |
| 13 | + result = f'{os.path.basename(path)} -> {realpath}' |
14 | 14 | return result
|
15 | 15 |
|
16 | 16 |
|
| 17 | +def get_file_info(path, root=None): |
| 18 | + """Return the file information including size and modification timestamp.""" |
| 19 | + if root is not None: |
| 20 | + path = os.path.join(root, path) |
| 21 | + size = os.path.getsize(path) |
| 22 | + modified = os.path.getmtime(path) |
| 23 | + modified_time = datetime.fromtimestamp(modified).strftime('%Y-%m-%d %H:%M:%S') |
| 24 | + return f'{realname(path, root=root)} (Size: {size} bytes, Modified: {modified_time})' |
| 25 | + |
| 26 | + |
17 | 27 | def ptree(startpath, depth=-1):
|
18 |
| - prefix = 0 |
| 28 | + """Generate the directory tree structure starting from the given path.""" |
19 | 29 | assert os.path.isdir(startpath), "Directory not valid"
|
20 | 30 | if startpath != '/':
|
21 | 31 | if startpath.endswith('/'):
|
22 | 32 | startpath = startpath[:-1]
|
23 |
| - prefix = len(startpath) |
| 33 | + |
24 | 34 | for root, dirs, files in os.walk(startpath):
|
25 |
| - level = root[prefix:].count(os.sep) |
| 35 | + level = root.count(os.sep) - startpath.count(os.sep) |
26 | 36 | if depth > -1 and level > depth:
|
27 | 37 | continue
|
28 |
| - indent = subindent = '' |
29 |
| - if level > 0: |
30 |
| - indent = '| ' * (level-1) + '|-- ' |
31 |
| - subindent = '| ' * (level) + '|-- ' |
32 |
| - print('{}{}/'.format(indent, realname(root))) |
| 38 | + |
| 39 | + indent = '| ' * level + '|-- ' |
| 40 | + print(f'{indent}{realname(root)}/') |
33 | 41 |
|
34 | 42 | for d in dirs:
|
35 | 43 | if os.path.islink(os.path.join(root, d)):
|
36 |
| - print('{}{}'.format(subindent, realname(d, root=root))) |
| 44 | + print(f'{indent}| {realname(d, root=root)}') |
| 45 | + |
37 | 46 | for f in files:
|
38 |
| - print('{}{}'.format(subindent, realname(f, root=root))) |
| 47 | + print(f'{indent}| {get_file_info(f, root=root)}') |
39 | 48 |
|
40 | 49 |
|
41 | 50 | if __name__ == '__main__':
|
| 51 | + print("\nDirectory tree\n") |
42 | 52 |
|
43 |
| - print("\nDirectory tree \n") |
44 |
| - |
45 |
| - parser = argparse.ArgumentParser(description='prints directory tree.') |
46 |
| - parser.add_argument('startpath', type=str, |
47 |
| - help='path to stating directory') |
| 53 | + parser = argparse.ArgumentParser(description='Prints directory tree.') |
| 54 | + parser.add_argument('startpath', type=str, help='Path to starting directory') |
| 55 | + parser.add_argument('-d', '--depth', type=int, default=-1, help='Depth of the directory tree') |
48 | 56 | args = parser.parse_args()
|
49 |
| - argsd = vars(args) |
50 |
| - ptree(**argsd) |
| 57 | + |
| 58 | + ptree(args.startpath, args.depth) |
51 | 59 |
|
52 | 60 | input("\n\nPress enter to exit")
|
0 commit comments