Iterate through the file with the open for close pattern

If you need to read from a file using Python, one way is to use the built-in open () command. Open a file called results . txt like this:

called VsultJ

result_f = open("results.txt")

The call to open () creates a file handle, which is a shorthand that you'll use to refer to the file you are working with within your code.

Because you'll need to read the file one line at a time, Python gives you the for loop for just this purpose. Like while loops, the for loop runs repeatedly, running the loop code once for each of the items in something. Think of a for loop as your very own custom-made data shredder:

The entire file is fed into the for loop shredder...

P"t the actual name of the tile -to open here.

Note: unlike a real shredder, the for loop shredder™ doesn't destroy your data—rb just Chops it into lines.

The for loop shredder™

The for loop shredder™

...which breaks it up into one-line-at-a-time chunks (which are themselves strings).

...which breaks it up into one-line-at-a-time chunks (which are themselves strings).

Each time the body of the for loop runs, a variable is set to a string containing the current line of text in the file. This is referred to as iterating through the data in the file: Open .the file *nd 9'Ve tyf it a file handle.

The "eachjine" v^Mc is result_f = open (" results . txt" )

set to the next line from —Do somexhing with the thing you've just read fv°m the file on each iteration. for each_line in result_f: * f^Z'ic-ls^, you print out the line. Notice

The for loop s"bops when print (each_line) that the for loop's code is indented.

you run out of lines to read. result_f.close () ^-Close the file (through the f ile handle)

when you're done with it.

Code Magnets

You need to complete the code to find the highest score in the results . txt file. Remember: the for loop creates a string from each line in the file.

Hint: For the program to work, you will need to convert the string into a number.

highest_score = 0

result_f = open("results.txt")

for line in result_f:

result_f.close()

print("The highest score was:")

print(highest_score)

getting the highest score

Code Magnets Solution

You needed to complete the code to find the highest score in the results . txt file. Remember: the for loop creates a string from each line in the file.

Hint: For the program to work, you needed to convert the string into a number.

Remember to indent the code inside the loop AND again inside the "¡¿"statement.

highest_score = 0 result_f = open("results.txt") for line in result f: if

Remember to indent the code inside the loop AND again inside the "¡¿"statement.

result_f.close()

print("The highest score was:")

The "highest_score" variabk gets updated every time you find a line that Contains a higher score.

highest_score result_f.close()

print("The highest score was:")

print(highest_score)

^ , « After the loop runs the whighest_sCore variable should have the best score W the defile, so you can then go ahead and d,splay it on screen-

Rememljer to Convert the string to a number with floatO- Even though "the |ine is a number, it Comes into the program as a string.

Was this article helpful?

0 0

Post a comment