forked from BaneZhang/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLs.py
More file actions
executable file
·114 lines (109 loc) · 3.76 KB
/
Copy pathLs.py
File metadata and controls
executable file
·114 lines (109 loc) · 3.76 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
import optparse
import os
import time
import locale
locale.setlocale(locale.LC_ALL, "")
def main():
parser = optparse.OptionParser("Usage: ls.py [options] [path1 [path2 [... pathN]]]")
parser.add_option("-H", "--hidden", action="store_true", dest="hidden",
help='show hidden files')
parser.add_option("-m", "--modified", action="store_true", dest="modified",
help='show last modified date/time')
parser.add_option("-o", "--order", type='choice', choices=['name', 'n', 'modified', 'm', 'size', 's'], dest="order", default="name",
help="order by ('name', 'n', 'modified', 'm', 'size', 's')[default: name]")
parser.add_option("-r", "--recursive", action="store_true", dest="recursive",
help='recurse into subdirectories [default: off]')
parser.add_option("-s", "--size", action="store_true", dest="size",
help='show sizes [default: off]')
opts, args = parser.parse_args()
if not args:
args = ['./']
else:
for i in args:
if not i.endswith('/'):
args[args.index(i)] += "/"
if opts.order in ['m', 'modified']:
mod = 'date'
elif opts.order in ['s', 'size']:
mod = 'size'
else:
mod = 'name'
line_keys = []
if not opts.recursive:
for n in args:
for i in os.listdir(n):
if opts.hidden:
info = {
'name': i,
'size': (os.path.getsize(n+i)),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(n+i))))
}
line_keys.append(info)
else:
if i.startswith('.'):
continue
else:
info = {
'name': i,
'size': (os.path.getsize(n+i)),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(n+i))))
}
line_keys.append(info)
else:
for i in args:
for root, dirs, files in os.walk(i):
gen1 = (x for x in dirs)
if opts.hidden:
for n in gen1:
info = {
'name': root + (n if root.endswith('/') else '/'+n),
'size': (os.path.getsize(root + (n if root.endswith('/') else '/'+n))),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(root + (n if root.endswith('/') else '/'+n)))))
}
line_keys.append(info)
#gen = (x for x in files if os.path.isfile(x) or os.path.isdir(x))
for n in files:
info = {
'name': root + (n if root.endswith('/') else '/'+n),
'size': (os.path.getsize(root + (n if root.endswith('/') else '/'+n))),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(root + (n if root.endswith('/') else '/'+n)))))
}
line_keys.append(info)
else:
for n in gen1:
if n.startswith('.'):
continue
else:
info = {
'name': root + (n if root.endswith('/') else '/'+n),
'size': (os.path.getsize(root + (n if root.endswith('/') else '/'+n))),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(root + (n if root.endswith('/') else '/'+n)))))
}
line_keys.append(info)
for n in files:
if n.startswith('.'):
continue
else:
info = {
'name': root + (n if root.endswith('/') else '/'+n),
'size': (os.path.getsize(root + (n if root.endswith('/') else '/'+n))),
'date': (time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(os.path.getmtime(root + (n if root.endswith('/') else '/'+n)))))
}
line_keys.append(info)
# print(line_keys)
if line_keys:
max_size = len(str(max(line_keys, key=lambda x: x['size']).get('size')))
else:
max_size = 0
if opts.size and opts.modified:
print_format = '{date} {size:>{ln}n} {name}'
elif opts.size:
print_format = '{size:>{ln}n} {name}'
elif opts.modified:
print_format = '{date} {name}'
else:
print_format = '{name}'
for i in sorted(line_keys, key=lambda x: x[mod]):
print(print_format.format(**i,ln=max_size+1))
main()