Ad Code

Responsive Advertisement

Data file operation

Data File Operation


Python File handling takes place in the following order

  • Opening a File
  • Performing File Operation (read, write, etc)
  • Closing a File

Types of Files in Python


There are three types of files in Python

  • Text File
  • Binary File
  • CSV File


Text Files

A Text File is a sequence of Characters which is ASCII or UNICODE. These files are Stored in Human Readable form and can be created using any text editor. These files are saved with extension .txt

Binary Files

A Binary File is used to store data like images, video files, etc. When we work on the binary file we have to interpret the raw bit pattern.


Opening and Closing File


Opening a File

When we read or write any file we need to open that file, which can be done using open() function.

Here we use open() function which takes two arguments that are the file name and file mode(which helps you to read or write data into the file).

Syntax for writing open() function is :

file = open ( "filename.txt",'file_mode' )


File Modes



FILE MODES

r

Open a file for reading only.


rb

Open a file for reading binary format.


r+

Open file for both reading and writing.


rb+

Open a file for reading and writing in binary format. 


w

Open a file for writing only.


wb

Open a file for writing only in binary format.


wb+

Open a file for both reading and writing in binary format.


a

Open a file for appending.


ab

Open a file for appending in binary format


a+

Open a file for appending and reading.


ab+

Open a file for both appending and reading in binary format.



Closing a File

The close method of the file flushes the unwritten data and closes the file after which no more data can be written.

Syntax of Closing a File


fileobject.close()

close() function breaks the link between the file-object and the file in the hard disk.

after close function, no task can be done using file-object.





In the above example, we have used the name object which will return the name of the file and in this case, the name of the file is diary.txt.

Let us discuss the Properties of the file objects-:


Once file is opened using open() function we can retrieve various information about the file let us see them one by one.

1. name


Name of the opened file. As we have discussed above the name of the file can be accessed by using .name after the file object. We can also access the file name after file is closed.













2. mode


It will tell the mode of the file, for example, read-only, write-only, write binary, etc.
Syntax -:

fileobject.mode

To learn modes of file click here -:



In the figure, we can see that print mode output is the same as the open mode which is 'r' .


3.  readable

It returns the boolean value (True/False) whether the file is readable or not.
Syntax-:

fileobject.readable()



4. closed

It returns the boolean value whether the file is closed or not.

Syntax-:
fileobject.closed




As you have seen in the above image that the fileobject.closed is giving False when close() function is not used but when close() function is used then the output is True.

Reading from a file.

We can read data from a file by various method. Here we will learn four ways to read data from a file.
Python provides following methods to read data from a file-:

This is data in the file named diary in txt form.




1. read()


read() method is used to read the entire day from a file.



2. read(n)

read(n) allows you to read the specific number of the characters from a file (n).



3. readline()

readline method read only first line from the file




4. readlines()

readlines() reads all the line present in a file.




Writing data into a file

 To write data into a file we have two methods-:


  • write()
  • writelines()

1. write()

write method takes strings as a argument and save it in a file. To store the data to the end of the line character we use \n at the end of the string.

Syntax-:

fileobject.write()

Let us make a file and fill data into it.

























































Post a Comment

0 Comments