Show
Ignore:
Timestamp:
03/05/10 21:38:58 (2 years ago)
Author:
hodgestar
Message:

File handling slide.

Location:
hodgestar/Talks/PythonForProgrammers
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • hodgestar/Talks/PythonForProgrammers

    • Property svn:ignore set to
      _*
  • hodgestar/Talks/PythonForProgrammers/slides/py4prog.py

    r726 r727  
    8484 
    8585    Slide("Course Structure", 
    86         Bullet("First Script:", PYCODE(''' 
     86        Bullet("First Script:", PYCODE(r''' 
    8787            #!/usr/bin/env python 
    8888 
     
    111111 
    112112    Slide("Numeric and Sequence Types", 
    113         Bullet("Numeric types:", PYCON(''' 
     113        Bullet("Numeric types:", PYCON(r''' 
    114114            >>> 5, 12345678901234567890123 
    115115            (5, 12345678901234567890123L) 
     
    135135    ), 
    136136 
     137    Slide("File Handling and Iterables", 
     138        Bullet("text2.py:", PYCODE(r''' 
     139            def main(args): 
     140                prog = args[0] 
     141                filenames = args[1:] 
     142 
     143                for filename in filenames: 
     144                    f = open(filename, "rb") 
     145 
     146                    try: 
     147                        for line in sorted(f): 
     148                            print line, 
     149                    finally: 
     150                        f.close() 
     151 
     152                    with open(filename, "rb") as f: 
     153                        for i, line in enumerate(sorted(f)): 
     154                            print "%3d: %s" % (i, line), 
     155 
     156                    with open(filename, "rU") as f: 
     157                        print f.read().replace("\n", r" \\ ") 
     158            ''')), 
     159    ), 
     160 
    137161) 
    138162