Docsity

Data file handling, Study notes of Computer science

Partial preview of the text

Download Data file handling and more Study notes Computer science in PDF only on Docsity!

CHAPTER - V

DATA FILE HANDLING

Unit I

Unit I

Programming and Computational

Thinking (PCT-2)

(80 Theory + 70 Practical)

DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)

Department of Computer Science, Sainik School Amaravathinagar

Cell No: 9431453730

DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)

Department of Computer Science, Sainik School Amaravathinagar

Cell No: 9431453730

Praveen M Jigajinni

Prepared byPrepared by

Courtesy CBSE

Class XII

LEARNING OUTCOMESLEARNING OUTCOMES

After going through the chapter, student will

be able to:

Understand the importance of data file for

permanent storage of data.

Understand how standard Input/Output

function work.

Distinguish between text and binary file

Open and close a file ( text and binary)

Read and write data in file

Write programs that manipulate data file(s

INTRODUCTION – DATA FILESINTRODUCTION – DATA FILES

A file (i.e. data file) is a named place on

the disk where a sequence of related data is

stored.

In python files are simply stream of

data, so the structure of data is not stored in

the file, along with data.

Basic operations performed on a data file are:

1.^ 1. Naming a fileNaming a file

2.^ 2. Opening a fileOpening a file

3.^ 3. Reading data from the fileReading data from the file

4.^ 4. Writing data in the fileWriting data in the file

5.^ 5. Closing a fileClosing a file

BASIC OPERATIONS ON FILE^ BASIC OPERATIONS ON FILE

FILE PROCESSINGFILE PROCESSING

Contd.. next

INTRODUCTION – DATA FILESINTRODUCTION – DATA FILES

6.^ 6. CREATE A COPY OF FILECREATE A COPY OF FILE

7.^ 7. UPDATING DATA IN THE FILE … etcUPDATING DATA IN THE FILE … etc

5.^ 5. DELETING DATA FROM FILEDELETING DATA FROM FILE

TYPES OF FILESTYPES OF FILES

What is Text File?

A text file is usually considered as sequence of

lines. Line is a sequence of characters (ASCII or

UNICODE), stored on permanent storage media.

The default character coding in python is ASCII each

line is terminated by a special character, known as

End of Line (EOL). At the lowest level, text file will

be collection of bytes. Text files are stored in

human readable form and they can also be created

using any text editor.

1.^ 1. TEXT FILETEXT FILE

What is Binary File?

A binary file contains arbitrary binary data i.e.

numbers stored in the file, can be used for

numerical operation(s). 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. In the case of binary file it is

extremely important that we interpret the correct

data type while reading the file. Python provides

special module(s) for encoding and decoding of

data for binary file.

2.^ 2. BINARY FILEBINARY FILE

OPENING AND CLOSING FILESOPENING AND CLOSING FILES

To handle data files in python, we need to

have a file object. Object can be created by

using open() function or file() function.

To work on file, first thing we do is open it.

This is done by using built in function open().

Syntax of open() function is

file_object = open(filename [, access_mode]

[,buffering])

OPENING AND CLOSING FILESOPENING AND CLOSING FILES

open() requires three arguments to work,

first one ( filename ) is the name of the file on

secondary storage media, which can be string

constant or a variable. The name can include the

description of path, in case, the file does not

reside in the same folder / directory in which we

are working

The second parameter (access_mode)

describes how file will be used throughout the

program. This is an optional parameter and the

default access_mode is reading.

OPENING AND CLOSING FILESOPENING AND CLOSING FILES

While writing the content in the file, first it

goes to buffer and once the buffer is full, data is

written to the file. Also when file is closed, any

unsaved data is transferred to file. flush()

function is used to force transfer of data from

buffer to file

FILE ACCESS MODESFILE ACCESS MODES