148k views
3 votes
Write a program that will take a file named Celsius.dat that contains a list of temperatures in Celsius (one per line), and will create a file Fahrenheit.dat that contains the same temperatures (one per line, in the same order) in Fahrenheit. Note: You may create Celsius.dat using a word processor or any other text editor rather than writing Python code to create the file. It simply needs to be a text file.

User Iban
by
6.0k points

1 Answer

1 vote

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.

  • # Release used resources

celciusFile.close()

fahrenheitFile.close()

All files are closed

User Ewokx
by
6.7k points