Difference between revisions of "CSC111 listmyprogram utility"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- Utility to list python programs (or text files) the same way they will be listed on printouts. Useful for spotting lines that are too long and that will be wrapped a…')
 
 
(One intermediate revision by the same user not shown)
Line 14: Line 14:
 
#      listmyprogram programname.py
 
#      listmyprogram programname.py
 
#
 
#
import textwrap
 
 
import sys
 
import sys
  
 +
# check command line parameters
 
if len( sys.argv ) < 2:
 
if len( sys.argv ) < 2:
 
     print "\nSyntax %s programname\n\n" % sys.argv[0]
 
     print "\nSyntax %s programname\n\n" % sys.argv[0]
 
     sys.exit( 1 )
 
     sys.exit( 1 )
  
 +
# open file
 
text = open( sys.argv[1], "r" ).read()
 
text = open( sys.argv[1], "r" ).read()
 +
 +
# split it into lines, and print each one, splitting it in half if
 +
# longer than 86 chars
 
lines = text.split( "\n" )
 
lines = text.split( "\n" )
 
for line in lines:
 
for line in lines:
Line 31: Line 35:
 
.
 
.
 
</source>
 
</source>
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]]

Latest revision as of 09:43, 1 March 2010

--D. Thiebaut 14:41, 1 March 2010 (UTC)


Utility to list python programs (or text files) the same way they will be listed on printouts. Useful for spotting lines that are too long and that will be wrapped around on the listing.

.

#! /usr/bin/python
# D. Thiebaut
# Utility to list python programs (or text files) the same way
# they will be listed on printouts.  Useful for spotting lines that 
# are too long and that will be wrapped around on the listing.
# Usage:
#      listmyprogram programname.py
#
import sys

# check command line parameters
if len( sys.argv ) < 2:
    print "\nSyntax %s programname\n\n" % sys.argv[0]
    sys.exit( 1 )

# open file
text = open( sys.argv[1], "r" ).read()

# split it into lines, and print each one, splitting it in half if
# longer than 86 chars
lines = text.split( "\n" )
for line in lines:
    if len( line ) >= 86:
        print line[:86]+"\n"+line[86:]
    else:
        print line

.