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\n" % fahrenheit)
# Release used resources
celciusFile.close()
fahrenheitFile.close()
Explanation:
<em></em>
<em>celciusFile=open("celcius.dat","r")
</em>
<em>fahrenheitFile=open("fahrenheit.dat","w")
</em>
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.
<em>
</em>
<em />
- <em># Iterate over each line in the file
</em>
<em>for line in celciusFile.readlines():
</em>
The values contained in the Celsius file are collected.
- <em># Calculate the fahrenheit
</em>
<em> fahrenheit = 9.0 / 5.0 * float(line) + 32
</em>
The values are then converted to Fahrenheit.
- <em>#write the Fahrenheit values to the Fahrenheit file</em>
<em> fahrenheitFile.write("%.1f\n" % fahrenheit)
</em>
The Fahrenheit values are written to the Fahrenheit file
<em>%.1f</em><em> is used to specify the number of digits after decimal. and </em><em>\n </em><em>adds a new line.</em>
<em />
- <em># Release used resources
</em>
<em>celciusFile.close()
</em>
<em>fahrenheitFile.close()
</em>
All files are closed
<em />