Skip to content

Commit 83c48fb

Browse files
Merge pull request #1593 from slayerrr12/master
Update tree.py
2 parents 56f8bfc + d82da0e commit 83c48fb

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

Directory Tree Generator/tree.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
1-
# Directory Tree Generator
2-
31
import os
42
import argparse
3+
from datetime import datetime
54

65

76
def realname(path, root=None):
7+
"""Return the real name of a path, including symbolic links."""
88
if root is not None:
99
path = os.path.join(root, path)
1010
result = os.path.basename(path)
1111
if os.path.islink(path):
1212
realpath = os.readlink(path)
13-
result = '%s -> %s' % (os.path.basename(path), realpath)
13+
result = f'{os.path.basename(path)} -> {realpath}'
1414
return result
1515

1616

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+
1727
def ptree(startpath, depth=-1):
18-
prefix = 0
28+
"""Generate the directory tree structure starting from the given path."""
1929
assert os.path.isdir(startpath), "Directory not valid"
2030
if startpath != '/':
2131
if startpath.endswith('/'):
2232
startpath = startpath[:-1]
23-
prefix = len(startpath)
33+
2434
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)
2636
if depth > -1 and level > depth:
2737
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)}/')
3341

3442
for d in dirs:
3543
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+
3746
for f in files:
38-
print('{}{}'.format(subindent, realname(f, root=root)))
47+
print(f'{indent}| {get_file_info(f, root=root)}')
3948

4049

4150
if __name__ == '__main__':
51+
print("\nDirectory tree\n")
4252

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')
4856
args = parser.parse_args()
49-
argsd = vars(args)
50-
ptree(**argsd)
57+
58+
ptree(args.startpath, args.depth)
5159

5260
input("\n\nPress enter to exit")

0 commit comments

Comments
 (0)