23 Class 12 FileHandling 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

SHANTINIKETAN WORLD SCHOOL, ALIGARH

CLASS XII/COMPUTER SCIENCE/FH1/2023-24

UNIT NAME: Computational Thinking and Programming-2


TOPIC: File Handling
SUBTOPICS: Text file, Binary file, CSV Files.

Data Files
Python provides inbuilt functions for creating, writing and reading files. There are two types
of files that can be handled in python, normal text files and binary files (written in binary
language,0s and 1s).
• Text files: In this type of file, each line of text is terminated with a special character
called EOL (End of Line), which is the new line character (‘\n’) in python by default.
• Binary files: In this type of file, there is no terminator for a line and the data is stored
after converting it into machine understandable binary language.

Text File
Regular Text Files
Most associate plain text files with the file extension. txt on Microsoft Windows computers,
however, can be any non-formatted file. To view a plaintext file, a text editor, such as
Microsoft Notepad is used.

Delimited Text Files


n these text files, a specific character is stored to separate the values, i.e., after each value, e.g.,
a tab or a comma after value.

TSV (Tab Separated Files): when a tab character is used to separate the values stored, these
are called TSV files. these files can take the extension as .txt or .csv.

CSV (Comma Separated Values files): when a comma character is used to separate the
values stored, these are called CSV files. these files can take the extension as .txt or .csv.

Binary Files:
A Binary file stores the information in the form of a stream of bytes.

Opening and closing files:


There are two types of files that can be handled in Python, normal text files and binary files
(written in binary language, 0s, and 1s).
Opening a file refers to getting the file ready either for reading or for writing. This can be done
using the open () function. This function returns a file object and takes two arguments, one
that accepts the file name and another that accepts the mode (Access Mode).

Note: The file should exist in the same directory as the Python script, otherwise, full address
of the file should be written.

Syntax: File_object = open (“File_Name”, “Access_Mode”)


Parameters:
• File_Name: It is the name of the file that needs to be opened.
• Access_Mode: Access modes govern the type of operations possible in the opened file.
The below table gives the list of all access mode available in python:

OPERATION SYNTAX DESCRIPTION

Read Only r Open text file for reading only.

Read and

Write r+ Open the file for reading and writing.

Write Only w/wb Open the file for writing.

Write and w+/w+b or Open the file for reading and writing. Unlike “r+” is doesn’t raise an

Read wb+ I/O error if file doesn’t exist.

Open the file for writing and creates new file if it doesn’t exist. All

additions are made at the end of the file and no existing data can be

Append Only a/ab modified.

Open the file for reading and writing and creates new file if it

Append and doesn’t exist. All additions are made at the end of the file and no

Read a+/ab+ existing data can be modified.

Example1: In this example, we will be opening a file to read-only.


# open the file using open () function
file = open("sample.txt")

Example2: In this example, we will open the file and printed its content.
# Reading from file
print (file.read ())

Example 3: In this example, we will be overwriting the contents of the sample file with the
below code:
# open the file using open () function
file = open ("sample.txt", 'w')

# Overwrite the file


file.write (" All content has been overwritten !")

Example 3: In this example, we will be overwriting the contents of the sample file with the
below code:
# open the file using open () function
file = open ("sample.txt", 'w')

# Overwrite the file


file.write(" All content has been overwritten !")

Closing a file in Python:


# open the file using open () function
file = open("sample.txt")

# Reading from file


print(file.read())

# closing the file


file.close()

Summarizes the open () function of Python:


F=open (“c:\\temp\\data.txt”, ‘r’)
Where, f file object created
“c:\\temp\\data.txt” path to file
r Mode

Working with Text File:


Reading from Text Files
1. Method: read ()
Syntax: <filehandle>.read([n])
Read at most n bytes; if no n is specified, read the entire file.
file 1= open (“E:\\mydata\\info.txt”)
readInfo=file1.read(15)
# 15 byte read
print (readInfo)

2. Method:readline()
Syntax: <filehandle>.readline([n])
Read line of input; if n is specified read at most n bytes.
Eg.,
file 1= open (“E:\\mydata\\info.txt”)
readInfo=file1.readline()
#1 line read
print (readInfo)

3. Method: readlines()
Syntax: <filehandle>.readlines()
Read all lines and returns them in a list.
Eg.,
file 1= open (“E:\\mydata\\info.txt”)
readInfo=file1.readline()
# all lines read
print (readInfo)

Example 1: Reading a file’s first 30 bytes and printing it.


myfile= open (r ‘E:\poem.txt’, “r”)
str=myfile.read (30)
print (str)

Example 2: Reading a file’s entire content


myfile= open (r ‘E:\poem.txt’, “r”)
str=myfile.read
print (str)
myfile.close()

Example 3: Reading a complete file-line by line.


myfile= open (r ‘E:\poem.txt’, “r”)
str= “ “
while str:
str=myfile.readline()
print (str, end= ‘ ‘)
myfile.close()

HOME ASSIGNMENT:

1. Write a program to display the size of a file in bytes.


2. Write a program to display the number of lines in the file.

You might also like