0% found this document useful (0 votes)
2K views56 pages

File Handling (Text, Binary, CSV) CLASS XII COMPUTER SCIENCE

The document discusses file handling in Python including opening and closing text files, reading from and writing to text files, binary files, CSV files, and standard input/output streams. It covers various file modes and methods like read(), readline(), readlines(), write(), writelines(), and flush().

Uploaded by

Dinesh Kumar Ram
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)
2K views56 pages

File Handling (Text, Binary, CSV) CLASS XII COMPUTER SCIENCE

The document discusses file handling in Python including opening and closing text files, reading from and writing to text files, binary files, CSV files, and standard input/output streams. It covers various file modes and methods like read(), readline(), readlines(), write(), writelines(), and flush().

Uploaded by

Dinesh Kumar Ram
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

KENDRIYA VIDYALAYA HINOO 1ST

SHIFT RANCHI
CHAPTER : FILE HANDLING IN PYTHON

Prepared By,
Dinesh Kumar Ram
PGT(CS)
LEARNING OBJECTIVES
At the end of this chapter you will be able to learn :

❏ Understanding Files
❏ Types of Files
❏ Understanding Text Files
❏ Opening and closing Text files
❏ Reading and writing in Files
❏ Understanding Binary Files
❏ Pickling and Unpickling
❏ Opening and closing Binary files
❏ Reading and writing in Binary Files
❏ CSV (Comma separated values) files. 2
NEED FOR DATA FILE HANDLING

● Mostly, in programming languages, all the values or data are stored in some
variables which are volatile in nature.
● Because data will be stored into those variables during run-time only and will be
lost once the program execution is completed. Hence it is better to save these data
permanently using files.

3
INTRODUCTION
● A file in itself is a sequence of bytes stored in some storage device like hard-disk,
pen-drive etc.
● Python allow us to create and manage three types of files :

1. TEXT FILE

2. BINARY FILE

3. CSV (Comma Separated Values) FILES

4
TEXT FILE
● A text file is structured as a sequence of lines.
● Line is a sequence of characters (ASCII or UNICODE)
● Stores information in ASCII or Unicode characters.
● Each line of text is terminated by a special character known as End Of Line
character.
● Text files are stored in human readable form and they can also be created using
any text editor.

5
BINARY FILE
● A file that contains information in the same format in which information
is held in memory.
● Binary file contains arbitrary binary data.
● So when we work on binary file, we have to interpret the raw bit
pattern(s) read from the file into correct type of data in our program.
● Python provides special module(s) for encoding and decoding of data
for binary file.

6
CSV FILES
● CSV stands for Comma Separated Values.
● CSV is just like a text file, in a human readable format which is extensively
used to store tabular data, in a spreadsheet or database.
● The separator character of CSV files is called a delimiter.
● Default delimiter is comma (,). Other delimiters are tab (\t), colon (:), pipe
(|), semicolon (;) characters.

7
8
STEPS TO PROCESS A FILE
1. Determine the type of file usage.
a. Reading purpose : If the data is to be brought in from a file to memory
b. Writing purpose : If the data is to be sent from memory to file.
2. Open the file and assign its reference to a file object or file-handle.
3. Process the file as required : Perform the desired operation from the file.
4. Close the file.

9
OPENING A TEXT FILE
● The key function for working with files in Python is the open() function.
● It accepts two parameters : filename, and mode.

Syntax:
<file_object_name> = open(<file_name>,<mode>)

Example: f = open(“[Link]”,”r”)

● Can specify if the file should be handled as binary or text Mode :


○ "t" - Text - Default value. Text mode
○ "b" - Binary - Binary mode (e.g. images)
10
● File Objects:
○ It serves as a link to file residing in your computer.
○ It is a reference to the file on the disk and it is through this link python
program can perform operations on the files.
● File access modes:
○ It governs the type of operations(such as read or write or append)
possible in the opened file.

11
Mode Description● Various Modes for opening a text file are:

“r” Read Default value. Opens a file for reading, error if the file does
not exist.

“w” Write Opens a file for writing, creates the file if it does not exist

“a” Append Opens a file for appending, creates the file if it does not exist

“r+” Read and File must exist otherwise error is [Link] reading and
Write writing operations can take place.

“w+” Write and File is created if it does not [Link] the file exists past data is
Read lost (truncated).Both reading and writing operations can
take place.

“a+” Append and File is created if it does not [Link] the file exists past data is
Read not lost .Both reading and writing(appending) operations
12
can take place.
CLOSING A FILE
● close()- method will free up all the system resources used by the file, this
means that once file is closed, we will not be able to use the file object any more.
● <fileobject>. close() will be used to close the file object, once we have finished
working on it.

Syntax:

<fileObject>.close()

Example : [Link]()

● It is important to close your files, as in some cases, due to buffering,changes


made to a file may not show until you close the file.
13
READING FROM A FILE
➔ A Program reads a text/binary file from hard disk. Here File acts like an input
to the program.
➔ Followings are the methods to read a data from the file:
◆ read() METHOD
◆ readline() METHOD
◆ readlines() METHOD

14
read() METHOD
● By default the read() method returns the whole text, but you can also specify
how many characters you want to return by passing the size as argument.

Syntax: <file_object>.read([n])

where n is the size

● To read entire file : <file_object>.read()


● To reads only a part of the File : <file_object>.read(size)

f = open("[Link]", "r")

print([Link](15))

Returns the 15 first characters of the file "[Link]". 15


16
readline() METHOD

● readline() will return a line read, as a string from the file.

Syntax:

<file_object>.readline()

● Example

f = open("[Link]", "r")

print([Link]())

● This example program will return the first line in the file “[Link]”
17
irrespective of number of lines in the text file.
Reading a complete file line by line using readline().

18
readlines() METHOD
● readlines() method will return a list of strings, each separated by \n
● readlines() can be used to read the entire content of the file.

Syntax:

<file_object>.readlines()

● It returns a list, which can then be used for manipulation.

Example : f = open("[Link]", "r")

print([Link]())
19
Read a file using Readlines()

20
21
WRITING TO A TEXT FILE
● A Program writes into a text/binary file in hard disk.
● Followings are the methods to write a data to the file.
○ write () METHOD
○ writelines() METHOD
● To write to an existing file, you must add a parameter to the open()

Function which specifies the mode :

○ "a" - Append - will append to the end of the file


○ "w" - Write - will overwrite any existing content

22
write() Method
● write() method takes a string ( as parameter ) and writes it in the file.
● For storing data with end of line character, you will have to add \n character to end
of the string
● Example:

Open the file "demo_append.txt" and append content to the file:

f = open("demo_write.txt", ""a)

[Link]("Hello students \n We are learning data file handling…..!")

[Link]()

f = open("demo_write.txt", "r") #open and read the file after the appending
23
print([Link]())
24
writelines() METHOD
● For writing a string at a time, we use write() method, it can't be used

for writing a list, tuple etc. into a file.

● Python file method writelines() writes a sequence of strings to the file.


The sequence can be any iterable object producing strings, typically a
list of strings.
● So, whenever we have to write a sequence of string, we will use
writelines(), instead of write().

25
26
The flush()
The flush function forces the writing of data on disc still pending on the
output buffer.

Syntax : <file_object>.flush()

27
28
Standard Input, Output, and Error
Streams
❏ Keyboard is the standard input device
❏ stdin - reads from the keyboard
❏ Monitor is the standard output device.
❏ stdout - prints to the display
❏ If any error occurs it is also displayed on the monitor and hence it is
also the standard error device.
❏ Same as stdout but normally only for errors (stderr)

The standard devices are implemented as files called streams. They can be
used by importing the sys module.
29
DATA FILE HANDLING IN BINARY
FILES
● Files that store objects as some byte stream are called binary files.
● That is, all the binary files are encoded in binary format , as a sequence of
bytes, which is understood by a computer or machine.
● In binary files, there is no delimiter to end the line.
● Since they are directly in the form of binary, there is no need to translate
them.
● But they are not in human readable form and hence difficult to

understand.

30
● Objects have a specific structure which must be maintained while storing or
accessing them.
● Python provides a special module called pickle module for this.

● PICKLING refers to the process of converting the structure to a byte

stream before writing to a file.

● UNPICKLING is used to convert the byte stream back to the original

structure while reading the contents of the file.


31
Pickling and Unpickling

32
PICKLE Module

● Before reading or writing to a file, we have to import the pickle


module.

import pickle

● It provides two main methods :


○ dump() method
○ load() method

33
Opening and closing binary files
Opening a binary file:

Similar to text file except that it should be opened in binary


[Link] a ‘b’ to the text file mode makes it binary - file mode.

EXAMPLE :

f = open(“[Link]”,”rb”)

Closing a binary file

[Link]()

34
Mode Description● Various Modes for opening a binary file are:

“rb” Read Default value. Opens a file for reading, error if the file does
not exist.

“wb” Write Opens a file for writing, creates the file if it does not exist

“ab” Append Opens a file for appending, creates the file if it does not exist

“r+b”or Read and File must exist otherwise error is [Link] reading and
“rb+” Write writing operations can take place.

“w+b”or Write and File is created if it does not [Link] the file exists past data is
“wb+” Read lost (truncated).Both reading and writing operations can take
place.

“a+b” or Append and File is created if it does not [Link] the file exists past data is
“ab+” Read not lost .Both reading and writing operations can take place.
35
[Link]() Method
● [Link]() method is used to write the object in file which is
opened in binary access mode.

Syntax :

[Link](<structure>,<FileObject>)

● Structure can be any sequence in Python such as list, dictionary etc.


● FileObject is the file handle of file in which we have to write.

36
binary_file.dat file
after execution of
the program.

37
[Link]() Method
● [Link]() method is used to read data from a file

Syntax :

<structure> = [Link](<FileObject>)

● Structure can be any sequence in Python such as list, dictionary etc.


● FileObject is the file handle of file in which we have to write.

38
39
Write a method to write employee
details into a binary file. Take the
input from the user.
Employee Details:

Employee Name

Employee Number

Department

Salary

40
Random Access in Files : tell() and seek()
❏ tell() function is used to obtain the current position of the file pointer

Syntax : [Link]()

❏ File pointer is like a cursor, which determines from where data has to be
read or written in the file.
❏ seek () function is used to change the position of the file pointer to a given
position.

Syntax : [Link](offset,reference_point)

Where f is the file object


41
CSV files
● CSV stands for Comma Separated [Link] is a type of plain text file that
uses specific structuring to arrange tabular data such as a spreadsheet or
database.
● CSV like a text file , is in a human readable format and extensively used to
store tabular data, in a spreadsheet or database.
● Each line of the file is a data record.
● Each record consists of one or more fields, separated by commas.
● The separator character of CSV files is called a delimiter.
● Default delimiter is (,).
● Other delimiters are tab(\t), colon (:), pipe(|), semicolon (;) characters.

42
Python CSV Module
● CSV module provides two types of objects :
○ reader : to read from cvs files
○ writer : to write into csv files.
● To import csv module in our program , we use the following statement:

import csv

43
Opening / Closing a CSV File
● Open a CSV File :

f = open(“demo_csv.csv”,”r”)

OR

f = open(“demo_csv.csv”,”w”)

● Close a CSV File:

[Link]()

44
Role of argument newline in opening
of csv files :
● newline argument specifies how would python handle new line characters
while working with csv files on different Operating System.
● Additional optional argument as newline = “”(null string) with the file
open() will ensure that no translation of EOL character takes place.

45
Steps to write data to a csv file
1. Import csv module

import csv

1. Open csv file in write mode.

f = open(“csv_demo.csv”,”w”)

1. Create the writer object.

demo_writer = [Link](f)

1. Write data using the methods writerow() or writerows()


demo_writer.writerow(<iterable_object>)
1. Close the file
[Link]() 46
Writing in CSV Files
● [Link]() :

Returns a writer object which writes data into csv files.

● writerow() :

Writes one row of data onto the writer object.

Syntax : <writer_object>.writerow()

● writerows() :

Writes multiple rows into the writer object

Syntax : <writer_object>.writerow() 47
Writing in CSV Files
● To write data into csv files, writer() function of csv module is used.
● [Link]():
○ Returns a writer object which writes data into writer object.

● Significance of writer object


○ The [Link]() returns a writer object that converts the data into a
delimited string.
○ The string can be later converted into csv files using the writerow()
or writerows() method.

48
● <writer_object>.writerow() :

Writes one row of data in to the writer object.

● <writer_object>.writerows()

Writes multiple rows into the writer object.

49
50
Reading from CSV Module
● To read data from csv files, reader function of csv module is used.

● [Link]() :

Returns a reader object.

It loads data from a csv file into an iterable after parsing delimited data.

51
Steps to read data from a csv file
1. Import csv module

import csv

1. Open csv file in read mode.

f = open(“csv_demo.csv”,”r”)

1. Create the reader object.

demo_reader = [Link](f)

1. Fetch data through for loop, row by row.

1. Close the file


[Link]() 52
53
QUESTIONS FOR
PRACTICE
Write a method in python to read the content from a file “[Link]”
and display the same on the screen.

Write a method in python to read the content from a file “[Link]”


line by line and display the same on the screen.

Write a method in python to count the number of lines from a text


file which are starting with an alphabet T.

Write a method in Python to count the total number of words in a file

54
Write a method in python to read from a text file “[Link]” to
find and display the occurrence of the word “Twinkle”

Write a method Bigline() in Python to read lines from a text file


“[Link]” and display those lines which are bigger than 50
characters.

Write a method to read data from a file. All the lowercase letter
should be stored in the file “[Link]” , all upper case characters
get stored inside the file “[Link]” and other characters stored
inside the file “[Link]”

55
THANK YOU

56

Common questions

Powered by AI

Text files are structured as a sequence of lines with data stored in a human-readable form using ASCII or Unicode characters. Each line is terminated by an end-of-line character . Binary files, however, contain data in the same format as in memory, which is not human-readable and often requires encoding and decoding through Python modules like 'pickle' . When handling text files, Python reads and writes using methods such as read(), readline(), and writelines() that process data as strings . In contrast, binary files are read and written using byte streams where the data is usually handled through serialization (pickling) and deserialization (unpickling). Text files use the default 't' mode, whereas binary files must be opened with 'b' mode .

CSV files, or Comma Separated Values files, store tabular data in a plain text format where each line is a record, and each record consists of fields separated by commas or other delimiters . Python handles CSV files by using the 'csv' module, which provides two primary objects: 'reader' for reading data and 'writer' for writing data into CSV files . To read a CSV file, the 'csv.reader()' method loads data into an iterable, and data is typically fetched row by row in a loop . To write to a CSV file, 'csv.writer()' creates a writer object that accepts data row-wise via 'writerow()' or multiple rows using 'writerows()' functions .

The 'pickle' module in Python is crucial for handling binary files when you need to serialize and deserialize Python objects. Pickling is the process of converting a Python object into a byte stream before writing it to a file, allowing the object's structure to be maintained . Unpickling reverses this process, converting the byte stream back to the original object structure upon reading from the file . This mechanism is essential for storing complex data types like lists and dictionaries in a file and is not applicable in text file handling where data is stored in a human-readable format .

Python's CSV module provides robust support for handling various delimiter types while reading from and writing into CSV files, allowing flexibility in parsing and outputting structured data . The default delimiter is a comma, but through the optional 'delimiter' parameter in 'csv.reader()' and 'csv.writer()' functions, it can be adjusted to accommodate tab ('\t'), colon, semicolon, or pipe ('|') delimiters . This feature is particularly significant for importing and exporting data between CSV files interpreted differently in various environments, ensuring interoperability with different data formats without data loss or corruption .

Python provides multiple methods for reading text files, each suited for different scenarios. The 'read()' method reads the entire file or a specified number of characters, ideal for processing files entirely or reading large segments at once . The 'readline()' method reads a single line at a time, useful for applications where processing data line-by-line is necessary, such as reading logs incrementally . 'readlines()' retrieves all lines as a list of strings, which is efficient for applications requiring access to lines as a list, enabling iteration or select line processing . These methods offer flexibility in handling files based on the application's purpose and resource constraints .

Standard file access modes in Python define how a file is to be interacted with during operations such as reading or writing. The primary modes include 'r' for read (an error is raised if the file doesn’t exist), 'w' for write (creates a file if it doesn’t exist but truncates it if it does), and 'a' for append (creates a file if it doesn’t exist and doesn’t truncate existing content). Modes 'r+', 'w+', and 'a+' allow simultaneous reading and writing operations with varying behaviors concerning existing content . These modes significantly impact file handling by determining whether data will be preserved, appended, or overwritten, and dictate whether an existing file is mandatory or not .

The 'flush()' method plays a crucial role in managing buffered output in file handling. This method forces any buffered output to be written to the file, ensuring data consistency and currency . Although files are usually managed by a buffer where data is temporarily stored before being written to the disk, in certain scenarios, such as critical data that must be saved immediately or log files where real-time updates are essential, flushing the buffer can be vital. This process eliminates the risk of data loss in case of unexpected program crashes or power failures .

The method would involve reading the content of the file, iterating through each character to determine its case, and then writing it to the corresponding output file ('lower.txt' for lowercase, 'upper.txt' for uppercase, and 'others.txt' for non-alphabetic characters). This type of data segregation requires understanding both reading and writing file operations as well as character type checking. The logic can be implemented using conditions to check if a character is lower or upper case or neither, employing the 'islower()', 'isupper()', and other suitable methods . The purpose is to separate data into categorized files based on the type of character, thus demonstrating nuanced file manipulation and condition-based classification .

The 'seek()' function is used to change the current position of the file pointer within a file, which is significant for non-linear data access. Its syntax is 'f.seek(offset, reference_point),' where 'offset' is the number of bytes to move, and 'reference_point' can be the beginning, current position, or end of the file . This ability to move the file pointer is crucial in scenarios where certain parts of a file need to be accessed more than others or when revisiting a specific section of the file without reopening it is required. It enhances file operation performance by reducing the need to read unnecessary parts of a file .

Not closing a file after performing operations in Python can lead to several implications. Primarily, it causes resource leaks where file descriptors remain occupied, potentially exhausting the number of available handles for the program . Additionally, due to buffering in file I/O operations, changes made to a file might not be written or saved until the file is closed, leading to data loss or inconsistency . Consequently, it is a best practice to always close files using the 'close()' method to ensure that all resources are freed and any buffered data is correctly written to disk .

You might also like