Ch 7 & 8
LIBRARY ROUTINES
FILE HANDLING
File Handling
Storing , modifying and retrieving data from/ to a file is
called File Handling.
Why do I need to store the data in a file ?
• Any data that is stored in RAM is lost when the
computer is switched off, when the data is saved in a
file, it is stored permanently.
•Data stored in a file can be accessed by
same/another program at a later date.
•Data stored in a file can be accessed by other
computer(s).
•The storage of data in file is one of the most used
features of programming.
Using files
• Every file is identified by its filename.
•In this section, we are going to look at how to read and
write a line of text or single item of data to a file.
Pseudocode
DECLARE TextLine : STRING
//Opening the file in Write mode and writing a line of text to it.
OPENFILE [Link] FOR WRITE
OUTPUT “Please enter a line of text”
INPUT TextLine
WRITEFILE [Link], TextLine
CLOSEFILE [Link]
//Opening the file in Read mode and reading a line of text from it.
OPENFILE [Link] FOR READ
READFILE [Link], TextLine
OUTPUT “The file contains this line of text”
OUTPUT TextLine
CLOSEFILE [Link]
Python Program
MyFile=open("[Link]“ ,"w")
TextLine=input("Please enter a line of text")
MyFile. Write(TextLine)
MyFile. close()
print("The file contains this line of text")
MyFile=open("[Link]“ ,"r")
TextLine=MyFile. read()
print(TextLine)
[Link]()