Reading and printing a file. : Readline : File PYTHON TUTORIALS


PYTHON TUTORIALS » File » Readline »

 

Reading and printing a file.


import sys

# open file
try:
   file = open"test.txt""r" )
except IOError:
   print >> sys.stderr, "File could not be opened"
   sys.exit)
   
records = file.readlines()   # retrieve list of lines in file

print "Account".ljust10 ),
print "Name".ljust10 ),
print "Balance".rjust10 )

for record in records:          # format each line
   fields = record.split()
   print fields].ljust10 ),
   print fields].ljust10 ),
   print fields].rjust10 )

file.close()



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


PYTHON TUTORIALS

 Navioo File
» Readline