File Handling Flashcards

All 6 cards in this deck

What does myFile.readLine() do?

Returns the next line (record) in the open file.

What does .writeLine() do?

e.g. myFile.writeLine("Sam")

Writes a line (record) into the open file — adds Sam as a new line.

What does .endOfFile() return?

True once the last record in the file has been read, otherwise false.

True or false? Data can be read from a file without opening the file first.

False. A file must be opened before reading or writing, and closed with .close() afterwards.

What are the steps to read and output every line of a text file?

e.g. the file data.txt

  1. Open it: myFile = open("data.txt")
  2. Loop to the end: while NOT myFile.endOfFile() … print(myFile.readLine()) … endwhile
  3. Close it: myFile.close()

What are the steps to create a new file and store a name typed in by the user?

e.g. the file names.txt

  1. Create it: newFile("names.txt")
  2. Open and write: myFile = open("names.txt") then myFile.writeLine(input("Name?"))
  3. Close it: myFile.close()