reading from a file
1. Download the rudyard.txt into the same directory as your file. (If using MU this has to be the default directory, normally mu_code)
1 The with command both opens and automatically closes the file. (Always try to use this format, its much safer than older Python.)
2 we use a for loop to step through the file. Python knows the length of the file, so you do not have to worry about the EOF.
3 we print the line. Notice the ,end="" because the file already has line breaks in it.
Exercise 1: Create an auto-prompter that lets you read the poem at a sensible pace.
You can use a variable to name your file. In this case I have used "my_filename".
with open(my_filename,"r") as whole_file:
Exercise 2: The female equivalent to this poem is mam.txt , create an auto-prompter that lets you choose the file that you want and auto prompts for you.
GIS SUBMIT>>
2 we use a for loop to step through the file. Python knows the length of the file, so you do not have to worry about the EOF.
3 we print the line. Notice the ,end="" because the file already has line breaks in it.
Exercise 1: Create an auto-prompter that lets you read the poem at a sensible pace.
You can use a variable to name your file. In this case I have used "my_filename".
with open(my_filename,"r") as whole_file:
Exercise 2: The female equivalent to this poem is mam.txt , create an auto-prompter that lets you choose the file that you want and auto prompts for you.
GIS SUBMIT>>