You are on page 1of 15

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.
The two types of Python data files:
TEXT file
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 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.

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 reads a string from an open file ,at most
n bytes from the file.
Examples: (1) line = fh.read(10) (2) line=fh.read()
(2) readline() method reads a line of string from an open
file ,at most n bytes from the file. 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.
(3) readlines() method reads all line of string from an
open file ,and returns them as a list , where each string will be
separated by \n. Example: line=fh.readlines()
Familiarizing
read()
# pyfiles4.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()
Size=len(line)
print(“Size of the file is ",Size ,”bytes”)

fh.close() # close the opened file


Familiarizing
readlines()
# pyfiles5.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()
Count=len(line)
print(“Number of lines in the file is ",Count)

fh.close() # close the opened file


FILE
WRITING METHODS

Python provides mainly two methods to write data to


a file.

They are
(1) write() method

(2)writelines() method
FILE
WRITING METHODS
(1) write() method
This method writes a string to a file referenced by
its file-handle/file object. The file may be a non-
existing or an existing one.

Syntax: file-handle.write(string)
Example: F.write(line)

NOTE: For writing data onto a text file, first open the
file in write mode, ”w”. If the file is an existing one,
data will be overwritten else creates a non-existing or a
new file.
FILE
WRITING METHODS
#pyfiles6.py
# To create a new text file

Fout= open("New.txt","w")
N=int(input("No.of lines:"))
for I in range(N):
Line=input("Statement:")
Fout.write(Line)
Fout.close()
FILE
WRITING METHODS
#pyfiles7.py
# To create a new text file

Fout= open("New2.txt","w")
N=int(input("No.of lines:"))
for I in range(N):
Line=input("Statement:")
Fout.write(Line)
Fout.write('\n')
Fout.close()

You might also like