0% found this document useful (0 votes)
7 views

Ch 7&8 - Library functions & File handling

Chapters 7 and 8 discuss file handling, which involves storing, modifying, and retrieving data from files to prevent loss when the computer is turned off. The document outlines the importance of file storage for data accessibility and provides pseudocode and a Python program example for reading and writing to files. Key operations include opening files in write/read mode, inputting data, and closing files after operations.

Uploaded by

virjatia1908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Ch 7&8 - Library functions & File handling

Chapters 7 and 8 discuss file handling, which involves storing, modifying, and retrieving data from files to prevent loss when the computer is turned off. The document outlines the importance of file storage for data accessibility and provides pseudocode and a Python program example for reading and writing to files. Key operations include opening files in write/read mode, inputting data, and closing files after operations.

Uploaded by

virjatia1908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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 File1.Txt FOR WRITE
OUTPUT “Please enter a line of text”
INPUT TextLine
WRITEFILE File1.Txt, TextLine
CLOSEFILE File1.Txt

//Opening the file in Read mode and reading a line of text from it.
OPENFILE File1.Txt FOR READ
READFILE File1.Txt, TextLine
OUTPUT “The file contains this line of text”
OUTPUT TextLine
CLOSEFILE File1.Txt
Python Program
MyFile=open("MyText.txt“ ,"w")
TextLine=input("Please enter a line of text")
MyFile. Write(TextLine)
MyFile. close()

print("The file contains this line of text")


MyFile=open("MyText.txt“ ,"r")
TextLine=MyFile. read()
print(TextLine)
MyFile.close()

You might also like