DataFilehandling_Text Files
DataFilehandling_Text Files
Beside above operations there are some more operations can be done on files.-
• Creating of Files
• Traversing of Data
• Appending Data into file.
• Inserting Data into File.
• Deleting Data from File.
• Copying of File.
• Updating Data into File.
File Types
File are of two types –
1. Text File: A text file is sequence of line and line is the sequence of characters and
this file is saved in a permanent storage device. Text file stores information in
ASCII OR UNICODE characters. In text file everything will be stored as a
character for example if data is “computer” then it will take 8 bytes and if the data
is floating value like 11237.9876 it will take 10 bytes. In text file each line is
terminated by special character called EOL. In text file some translation takes place
when this EOL character is read or written. In python EOL is ‘\n’ .These are in
human readable form and these can be created using any text editor.
2. Binary File: Binary files are used to store binary data such as images, videos audio
etc. Generally numbers are stored in binary files. In binary file, there is no delimiter
to end a line. Since they are directly in the form of binary hence there is no need to
translate them. That’s why these files are easy and fast in working.
Opening and Closing Files
• We need a file variable or file handle to work with files in Python.
• This file object can be created by using open( ) function or file( ) function.
• Open( ) function creates a file object, which is used later to access
the file using the functions related to file manipulation.
• Its syntax is following -
<file_object>=open(<file_name>,<access_mode>)
• File accessing modes –
▪ read(r): To read the file
Python External
▪ write(w): to write to the file Program File
▪ append(a): to Write at the end of file (Seconda
ry
Storage)
Read from
file (Load)
File Object Attributes
Once a file is opened and you have one file object, you can get various information related
to that file.
Here is a list of all attributes related to file object:
Attribute Description
file.closed Returns true if file is closed, false otherwise.
file.mode Returns access mode with which file was opened.
Python External
file.name Returns name of the file.
Program File
(Seconda
ry
Storage)
Opening and Closing Files
Here the point is that the file “Hello.txt” which is used here is pre
built and stored in the same folder where Python is installed.
File is closed
• Absolute path is the full address of any file or folder from the Drive i.e. from ROOT
FOLDER. It is like:
• Drive_Name:\Folder\Folder…\filename
• Relative Path is the location of file/folder from the current folder. To use Relative path
special symbols are:
• Single Dot ( . ) : refers to current folder.
• Double Dot ( .. ) : refers to parent folder
• Backslash ( \ ) : refers to ROOT folder.
File Modes
Mode Description
r To read the file which is already existing.
rb Read Only in binary format.
r+ To Read and write but the file pointer will be at the beginning of the file.
rb+ To Read and write binary file. But the file pointer will be at the beginning of the file.
w Only writing mode, if file is existing the old file will be overwritten else the new file will be created.
wb Binary file only in writing mode, if file is existing the old file will be overwritten else the new file
will be created.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file
does not exist, creates a new file for reading and writing.
wb+ Binary file only in reading and writing mode, if file is existing the old file will be overwritten else the
new file will be created.
a Append mode. The file pointer will be at the end of the file.
ab Append mode in binary file. The file pointer will be at the end of the file.
a+ Appending and reading if the file is existing then file pointer will be at the end of the file else new
file will be created for reading and writing.
ab+ Appending and reading in binary file if the file is existing then file pointer will be at the end of
the file else new file will be created for reading and writing.
Reading Files
To read from file python provide many functions like :
Filehandle.read([n]) : reads and return n bytes, if n is not specified it reads
entire file.
Filehandle.readline([n]) : reads a line of input. If n is specified reads at most
n bytes. Read bytes in the form of string ending with line character or blank
string if no more bytes are left for reading.
Filehandle.readlines(): reads all lines and returns them in a list
Reading a File
Output
1. Same readlines( )
function is used to
read many lines.
Program to count number of lines starting with a
f=open("Story.txt","r")
l=f.readlines()
print(l)
ctr=0
for i in l:
if i[0].lower()=='a':
ctr+=1
print("No of lines starting with a are",ctr)
f.close()
Program to count the frequency of word the in the file
demofile.txt
f = open("C:\\Users\\Naveen Arora\\Desktop\\demo.txt", "r")
ctr=0
x=f.read()
l=x.replace("\n"," ")
lst=l.split()
print(lst)
for i in lst:
if i.lower()=="the":
ctr+=1
print("the appears",ctr,"times")
f.close()
Writing to a File
We can write characters into file by using following two methods -
• write (string)
• writelines (sequence of lines)
write( ) : it takes a string as argument and adds to the file. We have to use ‘\n’ in string for end
of line character .
writelines() : if we want to write list, tuple into the file then we use writelines ( ) function.
“Hello.txt” File is
created using the
above program.
Writing to a File
Hello.txt file is opened using “with”.
Hello.txt file is opened using “with”.
Output
“Hello.txt” File is
created using the
above program.
Appending in a File
Append means adding something new to existing file. ‘a’ mode is used to accomplish this
task. It means opening a file in write mode and if file is existing then adding data to the
end of the file.
A program to append
into a file “Hello.Txt”
Output
print(f.closed)
Flush() Method
The method flush() flushes the internal buffer. It ensures whatever is held in
output buffer, is written to the actual file on disk
Python automatically flushes the files when closing them. But you may want to
flush the data before closing any file.
Syntax: fileObject.flush()
# Open a file
fo = open("f1.txt", "wb")
print ("Name of the file: ", fo.name)
# Here it does nothing, but you can call it with read operation.
fo.flush()
# Close opend
file fo.close()
Writing User Input to the File
Taking the data from the
user and writing this data Output
onto the file “Student.txt”
Student File is
created by using
the above
program.
File Pointer
Every file maintains a file pointer which tells the current position in the file where reading and
writing operation will take.
• We all know that the files are kept in directory which are also known as
folders.
• Every running program has a current directory which is generally a
default directory and python always see the default directory first.
• OS module provides many such functions which can be used to work
with files and directories. OS means Operating System.
• getcwd( ) is a very function which can be used to identify the current
working directory.
Drive
Folder
Relative and Absolute Paths
Relative and Absolute Paths
Absolute path is the full address of any file or folder from the Drive i.e. from
ROOT FOLDER. It is like:
Drive_Name:\Folder\Folder…\filename
For e.g. the Absolute path REVENUE.TXT will be
C:\SALES\2018\REVENUE.TXT
Absolute path of SEC_12.PPT is
C:\PROD\NOIDA\Sec_12.ppt
Relative Path is the location of file/folder from the current folder. To use
Relative path special symbols are:
• Single Dot ( . ) : single dot ( . ) refers to current folder.
• Double Dot ( .. ) : double dot ( .. ) refers to parent folder
• Backslash ( \ ) : first backslash before (.) and double dot( .. ) refers
to ROOT folder.
Relative and Absolute Paths
Suppose current working directory is : sales
We want to access sheet.Xls file, then relative
address will be
.\2019\sheet.Xls
• We use standard I/O Streams to get better performance from different I/O
devices.
• Some Standard Streams in python are as follows -
Output
OS Module
The OS module in python provides functions for interacting with the operating system.
OS, comes under Python’s standard utility modules. This module provides a portable way
of using operating system dependent functionality.
1. os.getcwd(): Function os.getcwd(), returns the Current Working Directory(CWD) of the
file used to execute the code, can vary from system to system.
import os
pwd = os.getcwd()
print("Current Directory :",pwd)
2. os.rename(): A file old.txt can be renamed to new.txt, using the function os.rename().
The name of the file changes only if, the file exists and user has sufficient privilege
permission to change the file.
import os
fd = "GFG.txt"
os.rename(fd,'New.txt')
OS Module
3. os.remove():To delete a file, you must import the OS module, and run its os.remove()
function:
import os
os.remove("demofile.txt")
4. os.path.exists(): It checks if the file exists.
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Practice Questions
Write a function in python to count the number of lines from a text file "story.txt"
which is not starting with an alphabet "T".
Example: If the file "story.txt" contains the following lines: A boy is playing there.
There is a playground.
An aeroplane is in the sky.
The sky is pink.
Alphabets and numbers are allowed in the password.
Practice Questions
def line_count():
file = open("story.txt","r")
count=0
for line in file:
if line[0] not in 'T':
count+= 1
file.close()
print("No of lines not starting with 'T'=",count)
line_count()