Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.mso.anu.edu.au/pfrancis/ObsTech/IO_demo.py
Дата изменения: Thu Apr 12 15:23:10 2007
Дата индексирования: Tue Oct 2 03:08:32 2012
Кодировка:

Поисковые слова: п п р р р р р р р р р р р р р р
# A program to demonstrate reading in an ASCII (text) file, doing something to it, then
# writing it out again to a different file.
# It reads the data from a file called data.d, which should consist of at least three numbers
# per line.
# It adds together the first two of these numbers. If the result is greater than the
# third number, it prints all three numbers out (to a file called results.d)
# Otherwise it prints out a rude message

# First, open the files
inhandle = open('data.d', 'r')
outhandle = open('results.d', 'w')

# Now loop through all the lines in the input.

for line in inhandle:
allarray = line.split() # Split the line you've just read in into its component bits
n1 = float(allarray[0]) # Convert the first component bit from a string to a number
n2 = float(allarray[1])
n3 = float(allarray[2])

sum = n1 + n2 # Add the first two numbers together

if sum > n3: # decide whether to print the numbers
print >> outhandle, n1, n2, n3
else: # otherwise print the rude message
print >> outhandle, 'No, you got it wrong'

# Finally close your connection to the files.

inhandle.close()
outhandle.close()