0% found this document useful (0 votes)
2 views1 page

File Handling in Python

File handling in programming allows data to be stored permanently in files, which can be accessed later or transferred to other computers. There are three primary operations for file handling: writing (overwriting existing data), appending (adding data to the end), and reading (retrieving data). The document provides examples of how to implement these operations using specific commands for opening, writing, appending, and reading files.

Uploaded by

Puravi Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

File Handling in Python

File handling in programming allows data to be stored permanently in files, which can be accessed later or transferred to other computers. There are three primary operations for file handling: writing (overwriting existing data), appending (adding data to the end), and reading (retrieving data). The document provides examples of how to implement these operations using specific commands for opening, writing, appending, and reading files.

Uploaded by

Puravi Chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File Handling

• Computer programs store data that will be needed again in a file.


• Data stored in RAM is volatile and will be lost when the computer is powered off.
• Data saved to a file is stored permanently, allowing it to be accessed by the same
program at a later date or by other programs.
• Stored data in a file can be transferred and used on other computers.
• The storage of data in files is a commonly used feature in programming.
Key point: When writing in a file, the program is outputing the data to the file, and when
reading a file, the program in inputing the data from the file
\n There are 3 ways a file can be opened in a program i.e. to write, to read and to append

Writing in a file
OPENFILE "filename.txt" FOR WRITE
//When opening a file to write, all the data already existing in the file is OVERWRITTEN
WRITEFILE "filename.txt" , Value
// The next command of WRITEFILE would be writen on next line of the file
CLOSEFILE "filename.txt"

Appending a file
OPENFILE "filename.txt" FOR APPEND
//When data will be written now, the file would be continued after the last line
WRITEFILE "filename.txt" , Value
// The next command of WRITEFILE would be writen on next line of the file
CLOSEFILE "filename.txt"

Reading a file:
OPENFILE "filename.txt" FOR READ
READFILE "filename.txt" , Variable
// The value in the line (which is identified by the number of times this is being run) is stored
in the variable
CLOSEFILE "filename.txt"

Reading a file till EOF:


OPENFILE "filename.txt" FOR READ
DECLARE DataVariable : STRING
WHILE NOT EOF("filename.txt) DO
READFILE "filename.txt", DataVariable
// here the line can be outputted or stored in an array. This process will repeat until every
line
//before the file ends has been read
ENDWHILE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy