Skip to content

Commit d58b991

Browse files
mimiron8080mimiron8080
authored andcommitted
0713
1 parent e8a469d commit d58b991

File tree

7 files changed

+110
-0
lines changed

7 files changed

+110
-0
lines changed

finally.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
# Filename: finally.py
3+
4+
import time
5+
6+
try:
7+
f = file('poem.txt')
8+
while True: # our usual file-reading idiom
9+
line = f.readline()
10+
if len(line) == 0:
11+
break
12+
time.sleep(2)
13+
print line,
14+
finally:
15+
f.close()
16+
print 'Cleaning up...closed the file'

picking.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
# Filename: picking.py
3+
4+
import cPickle as p
5+
# import pickle as p
6+
7+
shoplistfile = 'shoplist.data'
8+
# the name of the file where we will store the object
9+
10+
shoplist = ['apple','mango','carrot']
11+
12+
# Write to the file
13+
f = file(shoplistfile, 'w')
14+
p.dump(shoplist, f) # dump the object to a file
15+
f.close()
16+
17+
del shoplist # remove the shoplist
18+
19+
# Read back from the storage
20+
f = file(shoplistfile)
21+
storedlist = p.load(f)
22+
print storedlist

poem.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Programming is fun
2+
When the work is done
3+
if you wanna make your work also fun:
4+
use Python!

raising.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/python
2+
# Filename: raising.py
3+
4+
class ShortInputException(Exception):
5+
'''A user-defined exception class.'''
6+
def __init__(self, length, atleast):
7+
Exception.__init__(self)
8+
self.length = length
9+
self.atleast = atleast
10+
11+
try:
12+
s = raw_input('Enter something --> ')
13+
if len(s) < 3:
14+
raise ShortInputException(len(s), 3)
15+
# Other work can continue as usual here
16+
except EOFError:
17+
print '\nWhy did you do an EOF on me?'
18+
except ShortInputException, x:
19+
print 'ShortInputException: The input was of length %d, \
20+
was expecting at least %d' % (x.length, x.atleast)
21+
else:
22+
print 'No exception was raised.'

shoplist.data

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(lp1
2+
S'apple'
3+
p2
4+
aS'mango'
5+
p3
6+
aS'carrot'
7+
p4
8+
a.

try_except.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
# Filename: try_except.py
3+
4+
import sys
5+
6+
try:
7+
s = raw_input('Enter something --> ')
8+
except EOFError:
9+
print '\nWhy did you do an EOF on me?'
10+
sys.exit() # exit the program
11+
except:
12+
print '\nSome error/exception occurred.'
13+
# here, we are not exiting the program
14+
15+
print 'Done'

using_file.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/python
2+
# Filename: using_file.py
3+
4+
poem='''\
5+
Programming is fun
6+
When the work is done
7+
if you wanna make your work also fun:
8+
use Python!
9+
'''
10+
11+
f = file('poem.txt','w') # open for 'w' writing
12+
f.write(poem) # write text to file
13+
f.close() # close the file
14+
15+
f = file('poem.txt')
16+
# if no mode is specified, 'r'ead mode is assumed by default
17+
while True:
18+
line = f.readline()
19+
if len(line) == 0: # Zero length indicates EOF
20+
break
21+
print line,
22+
# Notice comma to avoid automatic newline added by Python
23+
f.close() # close the file

0 commit comments

Comments
 (0)