0% found this document useful (0 votes)
12 views

file handling

The document discusses data file handling, emphasizing the difference between transient and persistent programs, where persistent data is stored in permanent storage. It outlines the operations that can be performed on files, such as opening, reading, writing, and closing, as well as various file types like text and binary files. Additionally, it explains how to create file objects in Python using the open() function and describes different file access modes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

file handling

The document discusses data file handling, emphasizing the difference between transient and persistent programs, where persistent data is stored in permanent storage. It outlines the operations that can be performed on files, such as opening, reading, writing, and closing, as well as various file types like text and binary files. Additionally, it explains how to create file objects in Python using the open() function and describes different file access modes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data File Handling •

We have seen yet only the transient programs. The programs which run for a short period of time and give some
output and after that their data is disappeared. And when we again run those programs then we have to use new
data.

• This is because the data is entered in primary memory which is temporary memory and its data is volatile. • Those
programs which are persistent i.e. they are always in running or run for a long time then their data is stored in
permanent storage (e.g. harddisk) . If the program is closed or restarted then the data used will be retrieved.

• For this purpose the program should have the capability to read or write the text files or data files. These files can
be saved in permanent storage.

• The meaning of File I/O (input-output) is to transfer the data from Primary memory to secondary memory and vice-
versa.

Why the Files are used?

• The data stored with in a file is known as persistent data because this data is permanently stored in the system.

• Python provides reading and writing capability of data files.

• We save the data in the files for further use.

• As you save your data in files using word, excel etc. same thing we can do with python.

• “A File is a collection of characters in which we can perform read and write functions. And also we can save it in
secondary storage.”

Data File Operations

Following main operations can be done on files –

1. Opening a file

2. Performing operations

*2.1 READ WRITE etc.

3. Closing The File

Beside above operations there are some more operations can be done on files.-

• Creating of Files

• Traversing of Data

• Appending Data into file.

• Inserting Data into File.

• Deleting Data from File.

• Copying of File.

• Updating Data into File.

File Types

1. Text File: A text file is sequence of line and line is the sequence of characters and this file is saved in a permanent
storage device. Although in python default character coding is ASCII but by using constant ‘U’ this can be converted
into UNICODE. In Text File each line terminates with a special character which is EOL (End Of Line). These are in
human readable form and these can be created using any text editor.
2. Binary File: Binary files are used to store binary data such as images, videos audio etc. Generally numbers are
stored in binary files. In binary file, there is no delimiter to end a line. Since they are directly in the form of binary
hence there is no need to translate them. That’s why these files are easy and fast in working.

• We need a file variable or file handle to work with files in Python.

• This file object can be created by using open( ) function or file( ) function.

• Open( ) function creates a file object, which is used later to access the file using the functions related to file
manipulation.

• Its syntax is following - <file_object> = open(<file_name>,<access_mode>)

• File accessing modes –

– read(r): To read the file

– write(w): to write to the file

– append(a): to Write at the end of file.

You might also like