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
Samas 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- Open it:
myFile = open("data.txt") - Loop to the end:
while NOT myFile.endOfFile()…print(myFile.readLine())…endwhile - Close it:
myFile.close()
- Open it:
What are the steps to create a new file and store a name typed in by the user?
e.g. the file
names.txt- Create it:
newFile("names.txt") - Open and write:
myFile = open("names.txt")thenmyFile.writeLine(input("Name?")) - Close it:
myFile.close()
- Create it: