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
|
def print_file(name, mode='r'):
f = file(name, mode)
print f.read()
f.close()
print
# read inital file
print "initial_file.txt: read in r mode"
print_file("initial_file.txt", "r")
print "initial_file.txt: read in r mode again"
print_file("initial_file.txt", "r")
print "initial_file.txt: read in r+ mode"
print_file("initial_file.txt", "r+")
print "initial_file.txt: read in a+ mode"
print_file("initial_file.txt", "a+")
print "initial_file.txt: read in w+ mode"
print_file("initial_file.txt", "w+")
# simple write tests
f = file("file_1.txt", "w")
f.write("Write a new file, mode = 'w'\n")
f.close()
print "file_1.txt: write test"
print_file("file_1.txt")
f = file("file_2.txt", "w")
f.write("Write to file multiple times\n")
f.write("mode = 'w'\n")
f.close()
# read tests
print "file_2.txt: read in r mode"
print_file("file_2.txt", "r")
print "file_2.txt: read in r+ mode"
print_file("file_2.txt", "r+")
print "file_2.txt: read in a+ mode"
print_file("file_2.txt", "a+")
print "file_2.txt: read in w+ mode"
print_file("file_2.txt", "w+")
# append tests
f = file("file_3.txt", "w")
f.write("Text in w mode\n")
f.close()
f = file("file_3.txt", "a")
f.write("Text appended in\n")
f.write("a mode\n")
f.close()
print "file_3.txt: append test"
print_file("file_3.txt")
# rwa + modes tests here
# error tests
f = file("file_4.txt", 'w')
f.write("File for testing errors\n")
f.close()
print "Writing to read only files"
f = file("file_4.txt")
try:
f.write("a")
print "Test failed"
except Exception, e:
print e
f.close()
print
print "Reading non-existant files"
try:
file("non_existant.txt").read()
print "Test failed"
except Exception, e:
print e
print
print "Reading form write only files"
f = file("file_4.txt",'w')
try:
f.read()
print "Test failed"
except Exception, e:
print e
f.close()
print
print "Reading form append only files"
f = file("file_4.txt",'a')
try:
f.read()
print "Test failed"
except Exception, e:
print e
f.close()
|