Skip to content

Commit 7f8a1f7

Browse files
Directory Tree Generator files added
1 parent 1d5fb83 commit 7f8a1f7

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

Directory Tree Generator/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# DIRECTORY TREE GENERATOR
2+
3+
## Description
4+
A Script useful for visualizing the relationship between files and directories and making their positioning easy.
5+
- This can be used for implementations as one of the features.
6+
7+
### Language
8+
- [X] Python
9+
10+
### Instructions to run this application
11+
12+
1. Python 3 must be installed in your system.
13+
14+
- For first time, run this in terminal or powershell
15+
```
16+
pip3 install -r requirements.txt
17+
```
18+
2. It will download all the required modules
19+
20+
- Now run the below command
21+
```
22+
python tree.py ["LOCATION OF CURRENT DIRECTORY" ]
23+
```
24+
As an example
25+
```
26+
python tree.py C:\Users\kumar\Documents
27+
```
28+
Finally it will display you the tree of directory starting from the current directory.
29+
30+
Then press enter to exit.
67 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
argparse
2+
docopt

Directory Tree Generator/tree.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#Directory Tree Generator
2+
3+
import os
4+
5+
def realname(path, root=None):
6+
if root is not None:
7+
path=os.path.join(root, path)
8+
result=os.path.basename(path)
9+
if os.path.islink(path):
10+
realpath=os.readlink(path)
11+
result= '%s -> %s' % (os.path.basename(path), realpath)
12+
return result
13+
14+
def ptree(startpath, depth=-1):
15+
prefix=0
16+
if startpath != '/':
17+
if startpath.endswith('/'): startpath=startpath[:-1]
18+
prefix=len(startpath)
19+
for root, dirs, files in os.walk(startpath):
20+
level = root[prefix:].count(os.sep)
21+
if depth >-1 and level > depth: continue
22+
indent=subindent =''
23+
if level > 0:
24+
indent = '| ' * (level-1) + '|-- '
25+
subindent = '| ' * (level) + '|-- '
26+
print('{}{}/'.format(indent, realname(root)))
27+
28+
for d in dirs:
29+
if os.path.islink(os.path.join(root, d)):
30+
print('{}{}'.format(subindent, realname(d, root=root)))
31+
for f in files:
32+
print('{}{}'.format(subindent, realname(f, root=root)))
33+
34+
if __name__ == '__main__':
35+
import argparse
36+
37+
print("\nDirectory tree \n")
38+
39+
parser = argparse.ArgumentParser(description='prints directory tree.')
40+
parser.add_argument('startpath', type=str,
41+
help='path to stating directory')
42+
args = parser.parse_args()
43+
argsd=vars(args)
44+
ptree(**argsd)
45+
46+
input("\n\nPress enter to exit")

0 commit comments

Comments
 (0)