File Handling Interactive Activities

Build OCR Reference Language fluency through code-ordering tasks.

Activity 1: Parsons Problem - Write to a File

Drag each line into the answer area so the program creates a new file, writes to it, and closes it using OCR reference language.

Code Bank

Your Ordered Program

Tip: you can drag blocks between both boxes to reorder your answer.

Activity 2: Run OCR File-Write Code

Enter pilot code and date of birth, then run the OCR reference language program.

code = input("Enter code") dob = input("Enter date of birth") myFile = open("pilots.txt") myFile.writeLine(code + "," + dob) myFile.close()

Input Terminal

File Output View

pilots.txt

        

Activity 3: Parsons Problem - Read One Line at a Time

Arrange the OCR reference language code to read from a file one line at a time.

Code Bank

Your Ordered Program

Tip: loop condition first, then read inside the loop, and close the file at the end.

Activity 4: Run OCR File-Read Code

Edit pilots.txt on the right, then run the code to read one line at a time.

myFile = open("pilots.txt") while NOT myFile.endOfFile() print(myFile.readLine()) endwhile myFile.close()

        

Editable File View

pilots.txt