NCERTCh 2Free

File Handling in Python

🎓 Class 12📖 Computer Science📖 9 notes🧠 15 Q&A⏱️ ~14 min
h apterChapter 2 of 13Stack

File Handling in PythonStudy Notes

NCERT-aligned · 9 notes · 3 shown free

2.1 INTRODUCTION TO FILES

Explanation

2.1 INTRODUCTION TO FILES

In Python programming, data is usually stored temporarily in variables during program execution. These variables exist only while the program runs, and once the program terminates, the data stored in them is lost. This transient nature of variables poses a limitation when we want to store data permanently for future use. For example, organizations need to keep records of employees, inventory, sales, etc., permanently to avoid re-entering the same data repeatedly. To address this, data is stored permanently on secondary storage devices such as hard disks, SSDs, or USB drives. Files are the named locations on these secondary storage devices where data is stored permanently. Python programs themselves are stored as files with a .py extension. Similarly, input data and output generated by programs can be stored permanently in files for later access and reuse. This chapter introduces the concept of file handling in Python, which enables programs to create, read, write, and manipulate files stored on secondary storage. This capability is essential for developing real-world applications that require persistent data storage beyond the life of a program's execution.

  • Variables in a program exist only during execution; data is lost after program ends.
  • Permanent data storage is needed to reuse input and output data later.
  • Files are named locations on secondary storage devices where data is stored permanently.
  • Python programs are stored as files with .py extension on secondary storage.
  • File handling allows storing and retrieving data beyond program execution.
  • Organizations use files to store important data like employee records, inventory, and sales.
  • 📌 File: A named location on secondary storage where data is stored permanently.
  • 📌 Secondary Storage: Non-volatile storage devices like hard disks or SSDs used for permanent data storage.
  • 📌 File Handling: The process of creating, reading, writing, and manipulating files using programming.

2.2 TYPES OF FILES

Explanation

2.2 TYPES OF FILES

Files in computers are stored as sequences of bytes, which are binary digits (0s and 1s). Based on the nature of the stored data, files are broadly classified into two types: text files and binary files. Text files contain human-readable characters such as alphabets, digits, and special symbols. Examples include files with extensions like .txt, .py, .csv, etc. When opened with a text editor like Notepad, their contents appear as readable text. Internally, each character in a text file is stored as its ASCII or Unicode equivalent byte value. For example, the ASCII value 65 corresponds to the letter 'A'. Each line in a text file ends with a special character called the End of Line (EOL), which by default in Python is the newline character '\n'. Text files typically separate contents using whitespace, commas, or tabs. On the other hand, binary files store data in bytes that do not correspond to ASCII characters. These files contain data such as images, audio, video, executable programs, or compressed files. Binary files are not human-readable and require specific software to interpret their contents. Opening a binary file in a text editor usually shows garbage characters. Both text and binary files can be read and written in Python using appropriate modes. Understanding the difference between these file types is essential for handling data correctly in programs. **Table on page 4 (8×3)** | File Mode | Description | File Offset position | | --- | --- | --- | | <r> | Opens the file in read-only mode. | Beginning of the file | | <rb> | Opens the file in binary and read-only mode. | Beginning of the file | | <r+> or <+r> | Opens the file in both read and write mode. | Beginning of the file | | <w> | Opens the file in write mode. If the file already exists, all the contents will be overwritten. If the file doesn’t exist, then a new file will be created. | Beginning of the file | | <wb+> or <+wb> | Opens the file in read, write and binary mode. If the file already exists, the contents will be overwritten. If the file doesn’t exist, then a new file will be created. | Beginning of the file | | <a> | Opens the file in append mode. If the file doesn’t exist, then a new file will be created. | End of the file | | <a+> or <+a> | Opens the file in append and read mode. If the file doesn’t exist, then it will create a new file. | End of the file |

  • Files are stored as sequences of bytes (0s and 1s) on secondary storage.
  • Text files contain human-readable characters stored as ASCII or Unicode bytes.
  • Examples of text files: .txt, .py, .csv files.
  • Each line in a text file ends with an End of Line (EOL) character, usually '\n'.
  • Binary files contain non-human-readable data like images, audio, videos, executables.
  • Binary files require specific programs to read and write their contents.
  • 📌 Text File: A file containing human-readable characters stored as ASCII or Unicode bytes.
  • 📌 Binary File: A file containing data in binary form not representing readable characters.
  • 📌 End of Line (EOL): A special character marking the end of a line in a text file, default is '\n'.

2.3 OPENING AND CLOSING A TEXT FILE

Explanation

2.3 OPENING AND CLOSING A TEXT FILE

File operations in Python begin with opening a file using the open() function. The syntax is: file_object = open(file_name, access_mode). This function returns a file object (also called file handle) which acts as a link between the program and the f

Practice QuestionsFile Handling in Python

Includes NCERT exercise questions with answers

Q1.Among the following, which command is used to open a file “D:\TEMP\temp.txt” in append-mode?
A.f=open("D:\\TEMP\temp.txt","a")
B.f=open("D:\\TEMP\\temp.txt","w")
C.f=open(r "D:\TEMP\temp.txt","a")
D.f=open("D:\\TEMP\\temp.txt","w+")

Answer:

f=open(r "D:\TEMP\temp.txt","a")

MediumNCERT
Q2.Which of the following statements is true regarding the opening modes of a file?
A.When you open a file for writing, if the file does not exist, an error occurs.
B.When you open a file for writing, if the file does not exist, a new file is created.
C.When you open a file for reading, if the file does not exist, it will create a new file.
D.When you open a file for writing, if the file exists, the new content will be added at the end of the file.

Answer:

When you open a file for writing, if the file does not exist, a new file is created.

MediumNCERT
Q3.Suppose the file “story.txt” is stored inside the folder “D:\TEMP”.Which of the following command is used to open the file “story.txt” in write-mode only?
A.f = open(“D:\story.txt”, “w+”)
B.f = open(“story.txt”, “w”)
C.f = open("D:\TEMP\story.txt","wb")
D.f=open(r"D:\TEMP\story.txt","w")

Answer:

f=open(r"D:\TEMP\story.txt","w")

MediumNCERT
Q4.Which of the following command is used to open a file “D:\test.txt” in read-mode only?
A.f = open(“D:\test.txt”, “r”)
B.f = open(“D:\\test.txt”, “r”)
C.f = open(“test.txt”, “r”)
D.f = open(f = “D:\test.txt”, “r”)

Answer:

f = open(“D:\\test.txt”, “r”)

MediumNCERT
Q5.Which among the following is the correct syntax for opening a file?
A.file_object= open(file_name [, access_mode][, buffering])
B.file_object = open(file_name)
C.file_object = open(file_name ,[access_mode],[buffering])
D.None of the above

Answer:

file_object= open(file_name [, access_mode][, buffering])

MediumNCERT
Q6.Which command is used for writing sequence data types to a text file?
A.write()
B.writeline()
C.writelines()
D.writerow()

Answer:

writelines()

MediumNCERT
Q7.The default file open mode is:
A.write
B.read
C.append
D.read and write

Answer:

read

MediumNCERT
Q8.Which of the following commands can be used to read the entire contents of a file as a string using the file object <file>?
A.file.read(n)
B.file.read()
C.file.readline()
D.file.readlines()

Answer:

file.read()

MediumNCERT