Skip to content

Commit 83c113c

Browse files
author
xy
committed
chapter9
1 parent d005a09 commit 83c113c

16 files changed

Lines changed: 174407 additions & 0 deletions

File tree

chapter9/practice/9_1/9_1.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
file = raw_input('enter file path')
6+
fobj = open(file, 'r')
7+
i = 0
8+
for eachline in fobj:
9+
if eachline.strip(' ')[0] != '#':
10+
# if not eachline.startswith('#')
11+
i += 1
12+
print i
13+
14+
'''
15+
try to use startswith() and endswith()
16+
17+
'''

chapter9/practice/9_1/9_1.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# aaa
2+
# aaa
3+
ppp # aaa
4+
ppp
5+
ppp
6+
the true ans is 4

chapter9/practice/9_13.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
'''命令行参数。写一个程序,打印出所有的命令行参数。'''
6+
7+
import sys
8+
for item in sys.argv:
9+
print item

chapter9/practice/9_15/9_15.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
'''复制文件。提示输入两个文件名(或者使用命令行参数),把第一个文件的内容复制到第二个文件中去。'''
6+
7+
f1 = open(raw_input('enter file path 1: '))
8+
f2 = open(raw_input('enter file path 2: '), 'w')
9+
for i in f1:
10+
f2.write(i)
11+
12+
13+
'''
14+
rU 或 Ua 以读方式打开, 同时提供通用换行符支持 (PEP 278)
15+
w 以写方式打开,
16+
a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)
17+
r+ 以读写模式打开
18+
w+ 以读写模式打开 (参见 w )
19+
a+ 以读写模式打开 (参见 a )
20+
rb 以二进制读模式打开
21+
wb 以二进制写模式打开 (参见 w )
22+
ab 以二进制追加模式打开 (参见 a )
23+
rb+ 以二进制读写模式打开 (参见 r+ )
24+
wb+ 以二进制读写模式打开 (参见 w+ )
25+
ab+ 以二进制读写模式打开 (参见 a+ )
26+
'''

chapter9/practice/9_15/test1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
world
3+
xy

chapter9/practice/9_15/test2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello
2+
world
3+
xy

chapter9/practice/9_16/9_16.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
'''
6+
文本处理。
7+
人们输入的字符常常超过屏幕的最大宽度,编写一个程序,在一个文本文件中查找长度大于80个字符的文本行。
8+
#从最接80个字符的单词断行,把剩余的文件插入到下一行处。程序执行完毕后,应该没有超过80个字符的文本行了。
9+
'''
10+
11+
file = open
12+

chapter9/practice/9_4/9_4.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
def print_25_lines(file):
6+
num = 0
7+
for eachline in file:
8+
if num == 25:
9+
raw_input('press any key to continue')
10+
print_25_lines(file[25:])
11+
else:
12+
print eachline
13+
num += 1
14+
15+
file_list = open('./test_100lines.txt', 'r').readlines()
16+
print_25_lines(file_list)
17+
18+
'''
19+
with open("test_100lines.txt","a+") as fobj:
20+
21+
for i in range(100):
22+
23+
fobj.write(str(i))
24+
25+
fobj.write("\n")
26+
'''
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
1
2+
2
3+
3
4+
4
5+
5
6+
6
7+
7
8+
8
9+
9
10+
10
11+
11
12+
12
13+
13
14+
14
15+
15
16+
16
17+
17
18+
18
19+
19
20+
20
21+
21
22+
22
23+
23
24+
24
25+
25
26+
26
27+
27
28+
28
29+
29
30+
30
31+
31
32+
32
33+
33
34+
34
35+
35
36+
36
37+
37
38+
38
39+
39
40+
40
41+
41
42+
42
43+
43
44+
44
45+
45
46+
46
47+
47
48+
48
49+
49
50+
50
51+
51
52+
52
53+
53
54+
54
55+
55
56+
56
57+
57
58+
58
59+
59
60+
60
61+
61
62+
62
63+
63
64+
64
65+
65
66+
66
67+
67
68+
68
69+
69
70+
70
71+
71
72+
72
73+
73
74+
74
75+
75
76+
76
77+
77
78+
78
79+
79
80+
80
81+
81
82+
82
83+
83
84+
84
85+
85
86+
86
87+
87
88+
88
89+
89
90+
90
91+
91
92+
92
93+
93
94+
94
95+
95
96+
96
97+
97
98+
98
99+
99
100+
100

chapter9/practice/9_6/9_6.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
import hashlib
6+
7+
8+
def gethash(f):
9+
line = f.readline()
10+
hash = hashlib.md5()
11+
while(line):
12+
hash.update(line)
13+
line = f.readline()
14+
return hash.hexdigest()
15+
16+
17+
def is_hash_equal(f1, f2):
18+
str1 = gethash(f1)
19+
str2 = gethash(f2)
20+
return str1 == str2
21+
22+
if __name__ == '__main__':
23+
f1 = open("./a_.txt", "r")
24+
f2 = open("./a__.txt", "r")
25+
if is_hash_equal(f1, f2):
26+
print 'same!'
27+
else:
28+
lines1 = f1.readlines()
29+
lines2 = f2.readlines()
30+
for i in range(min(len(lines1), len(lines2))):
31+
if lines1[i] != lines2[i]:
32+
print i
33+
break
34+
35+
36+
'''
37+
compare two files' MD5 value
38+
'''

0 commit comments

Comments
 (0)