Wednesday, June 24, 2009

python open and read through file

# open file 'r' for read only 
# this assumes that myfile.txt is 
#   in the current working directory  
theFile = open('myfile.txt', 'r')
 
# iterate through the lines in the file object 
for line in theFile:
    # this is where you would perform logic on each line, or just print 
    print line
 
# close the file 
theFile.close()
 
#output: 
# Line one is here 
# line two is here 
# line 3 is here! 
 

No comments:

Post a Comment