python File handling | Binary files in python | Open files in python

we have discussed in detail about…

what are files?

what are text files?

types of text files?

Binary files

A binary file saves the information in the form of a collection of bytes.

A binary file will have information in the same form as the information is included in memory,

ie, the file data that is returned to us is raw to be more specific it has no translation or no specific encoding.

In a binary file, there is no limit for a line.

Also, no translations happen in binary files.

In conclusion, binary files are faster and simpler for a program to read and write than are text files.

As far as the file doesn’t need to be read by people or need to be transferred to a different type of system, binary files are the best way to store program information.

Also, binary files can take a variety of extensions.

Most non-text file extensions like any application-defined extension are binary files.

In conclusion, the text files can be opened in any text editor and they are human-readable but in the case of binary files are not in human-readable form.

Open and Close Files

To work with the files within the python program itself we need to open files in a particular mode for the appropriate operation

The most fundamental file usage tasks are like adding, modifying, or deleting data in a file,

which also include any one or the combination of the following operations. for example, adding and modify together or delete and modify together.

To open the file we have different modes :

  1. reading data from files
  2. writing data to files
  3. adding data to files

we are having built-in functions to carry on with these above-mentioned operations.

Opening files

For file handling in Python,

the first thing that we need to do is to open the file.

It can be done using the open() function.

bye using the proper syntax as mentioned below

Syntax

<file_objectname> = open(<filename>)
<file_objectname) = open(<filename>, <mode>)

Example

For example, 

myfile=open("mypython.txt")

as far now we have opened the file,

but In this case, as we have not specified the mode then python will take by default it as read mode but,

in case we need to open in any other mode we have to give specific information in which mode we are willing to open the file in python.

to specify the mode

Syntax

file1=open("mypython.txt")

file2 = open("data.txt","r")

file3 = open("e:\\main\\result.txt", "W")

In the first line, we are opening file “mypython.txt” in file mode as reading mode (default mode) and attaches it to the file object that we named as file1.

the second line, we open the file “data.txt” in read mode because of ‘r’ given as mode and attach it to the file object namely file2.

In the third line, we open file “result.txt” which is stored in folder E:\ main directory in write mode as we can see that we have given mode as “w” and associates it to file object namely file3.

To know more about the file handling operations in python:

CLICK HERE

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?