Grishachev - tree

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, sys
def rec1(path, ofile, prefix):
try:
print(u'{}├── {}'.format(prefix, os.path.basename(path)), file=ofile)
for item in os.listdir(path):
p = os.path.join(path, item)
if os.path.isdir(p):
rec1(path=p, ofile=ofile, prefix=prefix + u'│ ')
else:
print(u'{}│ ├── {}'.format(prefix, item), file=ofile)
except PermissionError:
print("Access denied by root!")
def print_dir_to_file(path, ofile, prefix=u''):
outfile = open(ofile, 'w')
rec1(path, outfile, prefix)
def rec2(path, ofile, prefix):
try:
print(u'{}├── {}'.format(prefix, os.path.basename(path)), file=ofile)
for item in os.listdir(path):
p = os.path.join(path, item)
if os.path.isdir(p):
rec2(path=p, ofile=ofile, prefix=prefix + u'│ ')
except PermissionError:
print("Access denied by root!")
def print_dir_only_tofile(path, ofile, prefix=u''):
outfile = open(ofile, 'w')
rec2(path, outfile, prefix)
def print_dir(path, prefix=u''):
try:
print(u'{}├── {}'.format(prefix, os.path.basename(path)))
for item in os.listdir(path):
p = os.path.join(path, item)
if os.path.isdir(p):
print_dir(p, prefix + u'│ ')
else:
print(u'{}│ ├── {}'.format(prefix, item))
except PermissionError:
print("Access denied by root!")
def print_dir_only(path, prefix=u''):
try:
sys.stdout.write(u'{}├── {}\n'.format(prefix, os.path.basename(path)))
for item in os.listdir(path):
p = os.path.join(path, item)
if os.path.isdir(p):
print_dir_only(p, prefix + u'│ ')
except PermissionError:
print("Access denied by root!")
key = sys.argv
if len(key) > 1:
if len(key) == 2:
if key[1] == '-d':
print_dir_only(path=os.getcwd())
elif os.path.isdir(key[1]):
print_dir(key[1])
else:
sys.stdout.write("Wrong format\n")
elif len(key) == 3:
if key[1] == '-o' and (not os.path.isdir(key[2])) and key[2] != '-d':
print_dir_to_file(ofile=key[2], path=os.getcwd())
elif key[1] == '-d' and os.path.isdir(key[2]):
print_dir_only(path=key[2])
else:
sys.stdout.write("Wrong format\n")
elif len(key) == 4:
if key[1] == '-o' and (not os.path.isdir(key[2])) and key[2] != '-d':
if key[3] == '-d':
print_dir_only_tofile(ofile=key[2], path=os.getcwd())
elif os.path.isdir(key[3]):
print_dir_to_file(ofile=key[2], path=key[3])
else:
sys.stdout.write("Wrong format\n")
elif key[1] == '-d' and key[2] == '-o' and (not os.path.isdir(key[3])) and key[3] != '-d':
print_dir_only_tofile(ofile=key[3], path=os.getcwd())
else:
sys.stdout.write("Wrong format\n")
elif len(key) == 5:
if key[1] == '-o' and (not os.path.isdir(key[2])) and key[2] != '-d' and key[3] == '-d' and os.path.isdir(key[4]):
print_dir_only_tofile(ofile=key[2], path=key[4])
elif key[1] == '-d' and key[2] == '-o' and (not os.path.isdir(key[3])) and key[3] != '-d' and os.path.isdir(key[4]):
print_dir_only_tofile(ofile=key[3], path=key[4])
else:
sys.stdout.write("Wrong format\n")
else:
sys.stdout.write("Wrong format\n")
else:
print_dir(os.getcwd())