Answer:
celciusFile=open("celcius.dat","r")
fahrenheitFile=open("fahrenheit.dat","w")
# Iterate over each line in the file
for line in celciusFile.readlines():
# Calculate the fahrenheit
fahrenheit = 9.0 / 5.0 * float(line) + 32
#write the Fahrenheit values to the Fahrenheit file
fahrenheitFile.write("%.1f\\" % fahrenheit)
# Release used resources
celciusFile.close()
fahrenheitFile.close()
Step-by-step explanation:
celciusFile=open("celcius.dat","r")
fahrenheitFile=open("fahrenheit.dat","w")
open the Celsius file in the read mode and assign it to a variable.
open the Fahrenheit file in the write mode and assign it to a variable.
read mode means you can only use/read the content in the file but cannot alter it.
write mode means you can write new data to the file.
- # Iterate over each line in the file
for line in celciusFile.readlines():
The values contained in the Celsius file are collected.
- # Calculate the fahrenheit
fahrenheit = 9.0 / 5.0 * float(line) + 32
The values are then converted to Fahrenheit.
- #write the Fahrenheit values to the Fahrenheit file
fahrenheitFile.write("%.1f\\" % fahrenheit)
The Fahrenheit values are written to the Fahrenheit file
%.1f is used to specify the number of digits after decimal. and \\ adds a new line.
celciusFile.close()
fahrenheitFile.close()
All files are closed