Skip to content

Commit 080588e

Browse files
Merge pull request avinashkranjan#553 from vikashkumar2020/tree
DIRECTORY TREE GENERATOR
2 parents b6adc71 + 6f11f9d commit 080588e

File tree

4 files changed

+80
-0
lines changed

4 files changed

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

0 commit comments

Comments
 (0)