File tree Expand file tree Collapse file tree 7 files changed +110
-0
lines changed Expand file tree Collapse file tree 7 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
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'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ Programming is fun
2
+ When the work is done
3
+ if you wanna make your work also fun:
4
+ use Python!
Original file line number Diff line number Diff line change
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 '\n Why 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.'
Original file line number Diff line number Diff line change
1
+ (lp1
2
+ S'apple'
3
+ p2
4
+ aS'mango'
5
+ p3
6
+ aS'carrot'
7
+ p4
8
+ a.
Original file line number Diff line number Diff line change
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 '\n Why did you do an EOF on me?'
10
+ sys .exit () # exit the program
11
+ except :
12
+ print '\n Some error/exception occurred.'
13
+ # here, we are not exiting the program
14
+
15
+ print 'Done'
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments