-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDay-4.1.py
More file actions
61 lines (50 loc) · 1.85 KB
/
CDay-4.1.py
File metadata and controls
61 lines (50 loc) · 1.85 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
# -*- coding: utf-8 -*-
import os
print os.listdir('/root') # like ls -a, including hidden dir
# use help ('os.walk') to get usage of walk
# ex from help ('os.walk')
import os
from os.path import join, getsize
result = ''
resultList = []
for root, dirs, files in os.walk('/root/'):
# result = root + ' consumes '
# result += str(sum([getsize(join(root, name)) for name in files]))
# result += ' bytes in' + str(len(files)) + ' non-dir files'
# print result
mysum = str(sum([getsize(join(root, name)) for name in files]))
mylen = str(len(files))
# todo: get AttributeError: __exit__ ?
# with open('/tmp/output.log', 'a').write('{0} consumes {1} bytes in {2} non-dir files\n'.format(root, mysum, mylen)):
# pass # close after write
# however, it's not good to open/close file frequently
# should cache all string and write to file once out of loop
# with open('/tmp/output.log', 'a') as f:
# f.write('{0} consumes {1} bytes in {2} non-dir files\n'.format(root, mysum, mylen))
result += '{0} consumes {1} bytes in {2} non-dir files\n'.format(root, mysum, mylen)
resultList.append('{0} consumes {1} bytes in {2} non-dir files\n'.format(root, mysum, mylen))
if '.git' in dirs:
dirs.remove('.git') # skip .git dirs
elif '.emacs.d' in dirs: # todo: should be more pythonic
dirs.remove('.emacs.d')
# print result
# print ''.join(resultList)
# test for list
# l = [1, 2, 3, 4]
# lc = l
# print l is lc
# l = []
# # del l[0:len(l)]
# # l[:] = []
# print l is lc
# print lc
# q6
result = []
with open('cdays-4-test.txt', 'r') as f:
result = f.readlines()
def myfilter(line):
line = line.strip()
return line and line[0] != '#' # empty string is falsy
result = filter(myfilter, result)
with open('cdays-4-result.txt', 'w') as f:
f.write(''.join(result))