Python files and example

 These different files in our daily life. For example, we may keep all our school W certificates in a file and call it a certificates file This file contains several certificates or pages and each page contains some data. From this example, we file is a place for storing data. Similarly, when we go to an office, we can simply say that can see a book that contains the attendance of the employees. This book is called an attendance file' since it contains the attendance data of employees. Let's understand that if the data is stored in a place, it is called a file.

 Files

 Data is very important. Every organization depends on its data for continuing its business operations. If the data is lost, the organization has to be closed. This is the reason computers are primarily created for handling data, especially for storing and retrieving data. In later days, programs are developed to process the data that is stored in the computer. To store data in a computer. we need files. For example, we can store employee data like employee number name, and salary in a file in the computer and later use it whenever we want. Similarly, we can store student data like student roll numbers, names,s, and marks on the computer In computer view a file is nothing but a collection of data that is available to a program. Once we store data in a computer file, we can retrieve it and use it depending on our requirements. Figure 171 shows the file in our daily life and the file stored on the computer:

Types of Files in Python 
In Python, there are two types of files. They are Text files Binary files Text files store the data in the form of characters. For example, if we store the employer name "Ganesh" it will be stored as 6 characters, and the employee salary 8900.75 is stored as 7 characters. Normally text files are used to store characters or strings. Binary files store entire data in the form of bytes, 1. e a group of 8 bits each. For example, a character is stored as a byte and an integer is stored in the form of 8 bytes (on a 64 bit

Opening a aile We should use an open() function to open a file. This function accepts a filename and open mode in which to open the file handler = open("filename" "open mode "buffering") Here, the file name' represents a name on which the data is stored. We can use any name to reflect the actual data. For example, we can use 'empdata' as the file name to represent the employee data. The file 'open mode represents the purpose of opening the file Table 17 specifies the file open modes and their meanings:

Closing a File 
A file that is opened should be closed using the close) method Once a file is opened but not closed, then the data of the file may be corrupted or deleted in some cases Ale if the file is not closed the memory utilized by the file is not freed leading to problems like insufficient memory This happens when we are working with several files simultaneously Hence it is mandatory to close the file. fclose() Here the file represented by the file object T is closed It means T is deleted from the memory Once the file object is lost. file data will become inaccessible If we want to do any work with the file again, we should once again open the file using the open function In the Program we are creating a file where we want to store some characters. We know that a group of characters represents a string After entering a string from the keyboard using input() function we store the string into the file using write() method as 
f.write(str)

working with Binary Files

handle data in the form of bytes. Hence, they can be used to

audio and video files. To open a binary file for reading upon de Here is attached to Y to represent that it

I binary Similarly it into a binary file, we can use mode Terabytes from a binary file e the read method and to write bytes into a binary file, we can use the write

w program where we want to open an image slice perp le and babies from that file. These bytes are then written into a new binary nie. It means copying an image file as another file. This is shown in Program

8: A Python program to copy an image file into another file

copying an image into a file open the files in binary mode 11 - opens.cat.jpg 12 12 - open new pow

read bytes from fl and write into 12 bytesti.reado

write(bytes) close the files

close close

So pthon copy. by

In this pro

en directory Current directo

dove program is run, it will copy the image file at tender til program, our assumption is that there

is also in tory. The current directory in the directory where CLIPE is not available in the ent directory, but

we have to supply the path of that directory to open hance

nest.

The seek() and tell() Methods

We knew that date in the binary files is stored in the form of brytes. We

or writing operations on a binary file, le pointer mires depending on how many bytes are written or read from the file. For 10 bytes of data from a file, the file pointer will be positioned at the 10 possible to continue reading from the 11 byte onwards. To know the pointer, we can use the tell method. It returns the current position of the from the beginning of the file. It is used in the form:

For example,

pen of these

The r tensile handler or file object is an integer that represents a position where the file pointer is positioned In case we want to move the file pointer to another position, we can the method. This method takes two arguments

Sees offset, Frohere) Here, bet represents how many bytes to move from where represents from the position to move. For example, from where can be 0, 1, or 2. Here, represents the true beginning of the file. 1 represents the current position and 2 represents the enting of the file. The default value of from where is O Le beginning of the file

10)

Santas f.seekco.0) This will move the file pointer to the 11 bytes file 10+11 from the beginning of the present beginning of the few 50, any reading operation will read data from

This will mot the file pointer to the byte 1-10+1) from the ending of represents the ending of The negative ser before 10 represents moving

the ending of the file

moving back in the

We will take an example to understand how these methods work in case

the following code snippet where we open a binary file line of that it is possible to write data into the file and read data from the file

with online.txt' b') as fi

In the case of a binary debate in episode 30

with the strings. It

Random Accessing of Binary Files using a map

my mapped the module in Python that is useful in and manipulating the data of the file as we do with the string y e

n d with some data, that data is viewed member ng map module. The first step is to use the map mode using the map methods -

fileno .0) The me the currently opened file with the file object mm. Pe arguments mad method. The first argument is 1.fileno). This indicates

handle to the file object 1. This represents the actual binary file m ed The second argument is zero ) represents the total of the

red for mapping So, the entire file is represented by the file objects more to the object mm. This means, mm' will now onwards behave like the Now, we can read the data from the file using read) or reading methods

print .reado) displays entire le data

printrendline) aisplays the first line of the file ise, we can retrieve data from the file using slicing operators

print3 2 print

display from 5 :161)

byte till the end

display from th to 90 bytes It is possible to modify or replace the data of the file using slicing as 10

replace from 5 to 90 characters by stri We can also use find method that returns the first position of a string in the

return the position of name the

We can also u se method to position the file pointer to any position

S o , position the file pointer to 100 bytet

beginning of file Let's remember that the memory mapping is doneonly for the binary nie text files. So, first of all let's create a binary file where we want to Suppose we want to store name and phone number into the file.

web or a lerary mas

Methore

files and more to store some e, we can use

fatitename phone)



Post a Comment

0 Comments