file handling
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.
• The data stored with in a file is known as persistent data because this data is permanently stored in the system.
• 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.”
1. Opening a file
2. Performing operations
Beside above operations there are some more operations can be done on files.-
• Creating of Files
• Traversing of Data
• Copying of 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.
• 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.