You are on page 1of 19

A high-level, interpreted

and general-purpose dynamic programming language


that focuses on code readability
DATA FILE
HANDLING
Data File handling
in
Python
File: It is a stream of bytes stored permanently on some
secondary storage devices for future use. It can be perceived as
a set of records, where each record has some fields which in
turn store data.
The Python language supports two types of data files:
TEXT file and BINARY file

• Text file: A text file stores information in readable and


printable form. In text files, each line of text is terminated with
an EOL (End of Line) character or delimiter character(\n).
When this EOL character is read or written, certain internal
translations take place. The file extension of text file is .txt.
Basic Data File
Operations in
Python

In order to work with a file from within a Python program, we


have to open it in a specific access mode (related to the task
to be performed).

The most basic file manipulation tasks include adding,


modifying or deleting data in a file, which in turn include
any one or combination of the following operations:
• Reading data from file
• Writing data to file
• Appending data to file

Python provides built-in functions to perform each of these


tasks. The built-in function used to open a file is open()
File Access Modes
in
Python

File Access Modes


Access modes govern the type of operations possible in the opened
file. It refers to how the file will be used once its opened. These
modes also define the location of the File Handle in the file. File
handle is like a cursor, which defines from where the data has to be
read or written in the file.
There are 6 access modes in python.
1) Read Only (‘r’)
2) Read and Write (‘r+’)
3) Write Only (‘w’)
4) Write and Read (‘w+’)
5) Append Only (‘a’)
6) Append and Read (‘a+’)
The built-in function
open()

Python provides built-in functions for creating, writing and


reading files.
Opening a File: Before performing any operations on a file, first
we need to open the file. We use open () function in Python to open
a file in read/write/append mode and open( ) will return a file
object/handle which acts as a reference to the file on the disk. The
open() function accepts two arguments, file name and the access
mode(read or write).
Syntax: File_object = open("File_Name","Access_Mode")
Example: (1) f=open(“Data.txt”, ”r”) # same directory
(2) f=open(r”C:\temp\data.txt”, ”r”) #different directory
(3) f=open(”C:\\temp\\data.txt”,”r”) #different directory
The close()
method

Closing a File:
It is done using the close() method of its file-object/ file
handle.
It breaks the link of the file-object/ file handle and
the file on the disk.
It is very important that an opened file has to be closed
at the end of its use in the program.
In Python, files are automatically closed at the end of the
program but it is a good practice to close the files
explicitly.

Syntax: File handle. close()


Example: F.close() # F is the file handle/object
FILE
READING METHODS

Python provides mainly three methods to read data


from a file. It is important that the file must be opened
and linked through a file handle/object before
reading.

The three methods to read a file are


(1) read() method

(2) readline() method

(3) readlines() method


FILE
READING METHODS
The read() Method
Python file method read() reads a string from an open file ,at
most n bytes from the file.

If the read hits EOF before obtaining size bytes, then it reads only
available bytes.

If n is not specified, the entire file will be read.


It is important to note that Python strings can have binary data.
apart from text data.

Syntax: fileObject.read([n])
FILE
READING METHODS
The read() Method

Syntax: fileObject.read([n])

Here, passed parameter is the number of bytes to be read from


the opened file.
This method starts reading from the beginning of the file and if
count is missing, then it tries to read as much as possible, maybe
until the end of file.

Examples:
(1) line = fh.read(10)
(2) line=fh.read()
FILE
READING METHODS
# pyfiles1.py
fh = open("c:\\temp\\data.txt", "r")
print("Name of the file: ", fh.name)
# The text file has following 4 lines
#A specimen for a text file is in a different folder.
#The name of the folder is Temp.
#The drive is C:
#And the path of the file is C:\temp\data.txt

line = fh.read(10)
print("Read Line:",line)

# close the opened file


fh.close()
FILE
READING METHODS
The readline() Method

Python file method readline() reads a line of string from an open


file ,at most n bytes from the file. If the read hits EOL before
obtaining size bytes, then it reads only available bytes.

If n is not specified, the first line of the file will be read.


A similar second statement will return second line and so on.

In this method, a line is considered till a newline character(\n) is


encountered in the data file.
Syntax: fileObject.readline([n])
FILE
READING METHODS

The readline() Method

Syntax: fileObject.readline([n])

Here, passed parameter is the number of bytes to be read from


the opened file.
This method starts reading from the beginning of the file and if
count is missing, then it tries to read the first line of the file.

Examples:
(1) line = fh.readline(10)
(2) line=fh.readline()
FILE
READING METHODS
# pyfiles2.py
fh = open("c:\\temp\\data.txt", "r")
print("Name of the file: ", fh.name)
# The text file has following 4 lines
#A specimen for a text file is in a different folder.
#The name of the folder is Temp.
#The drive is C:
#And the path of the file is C:\temp\data.txt

line = fh.readline(10)
print("Read Line:",line)

# close the opened file


fh.close()
FILE
READING METHODS
The readlines() Method

Python file method readlines() reads all line of string from an


open file ,and returns them as a list. readlines()can be used to
read the entire content of the file. The method will return a list of
strings, each separated by \n.
Syntax: fileObject.readlines()

This method starts reading from the beginning of the file till the
end and returns them as a list.

Example:
line = fh.readlines()
FILE
READING METHODS
# pyfiles3.py
fh = open("c:\\temp\\data.txt", "r")
print("Name of the file: ", fh.name)
# The text file has following 4 lines
#A specimen for a text file is in a different folder.
#The name of the folder is Temp.
#The drive is C:
#And the path of the file is C:\temp\data.txt

line = fh.readlines()
print("Read Line:",line)

# close the opened file


fh.close()
FILE
READING METHODS
# pyfiles3.py
fh = open("c:\\temp\\data.txt", "r")
print("Name of the file: ", fh.name)
line = fh.readlines()
print("Read Line:",line)
fh.close()
Output:
Name of the file: c:\temp\data.txt
Read Line: ['A specimen for a text file in a different folder.\n',
'The name of the folder is Temp.\n', 'The drive is C:\n', 'And
the path of the file is C:\\temp\\data.txt']

NOTE: The readlines() method will return a list of strings,


each separated by \n
FILE
READING METHODS

Python provides mainly three methods to read data


from a file.

They are
(1) read() method

(2) readline() method

(3) readlines() method

You might also like