0% found this document useful (0 votes)
2 views

DataFilehandling_Text Files

The document discusses data file handling in programming, emphasizing the difference between transient and persistent data storage. It covers file operations such as opening, reading, writing, and closing files, as well as file types (text and binary) and file modes. Additionally, it introduces Python's capabilities for file manipulation, including the use of the OS module for file management and best practices like using the 'with' statement for file handling.

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DataFilehandling_Text Files

The document discusses data file handling in programming, emphasizing the difference between transient and persistent data storage. It covers file operations such as opening, reading, writing, and closing files, as well as file types (text and binary) and file modes. Additionally, it introduces Python's capabilities for file manipulation, including the use of the OS module for file management and best practices like using the 'with' statement for file handling.

Uploaded by

Soham Mukherjee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Data File Handling

Data File Handling


• We have seen yet only the transient programs. The programs which run for a short period of time
and give some output and after that their data is disappeared. And when we again run those
programs then we have to use new data.
• This is because the data is entered in primary memory which is temporary memory and its data is
volatile.
• Those programs which are persistent i.e. they are always in running or run for a long time then
their data is stored in permanent storage (e.g. harddisk) . If the program is closed or restarted then
the data used will be retrieved.
• For this purpose the program should have the capability to read or write the text files or data files.
These files can be saved in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data from Primary memory to secondary
memory and vice-versa.
(Random
ProgramAccess
in RAM Hard
User
Memory) Disk
Files Introduction
• The data stored with in a file is known as persistent data because this data is permanently
stored in the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same thing we can do with python.
• “A File is a collection of characters in which we can perform read and write functions. And
also we can save it in secondary storage.”
Write to file
(Save)
Python External
Program File
(Secondar
y Storage)
Read from
file (Load)
Data File Operations
The main operations that can be done on files are -
1. Opening a file
2. Performing operations- Read and Write
3. Closing the file
Open File Process Data Close File

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.

Opened the File

File is closed

A program describing the functions of file handling. Output


Absolute and Relative Path of File

• 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

Notepad File saved in the


same folder as .py file
Reading a File
Reading first 10 Output
characters from the
file “Hello.txt”

1. We can also use


readline( ) function
which can read
one line at a time
from the file.

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.

This “Hello.txt” is created using this


program.
Writing to a File

A Program to use writelines()


function
Output

“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

A new data is appended into


Hello.txt by above program.
Seek() Method
To read or write at a specific position, use the seek() function to set the current read/write
position.
• f.seek(offset, from)
• Here, the from parameter takes the following values:
• 0 : offset calculated from the beginning
• 1 : offset calculated from the current position,
• 2 : offset calculated from the end
Assuming that myfile.txt contains "Hello World" text, the following example demonstrates the
seek() method.
f=open("D:\myfile.txt","r+")
f.seek(6,0)
lines=f.readlines()
for line in lines:
print(line)
f.close()
with Statement
It is ‘best practice’ to always open files using the with statement. With automatically closes
your file as soon as your code is finished with it. Just like the for and if statements we have
already encountered, with ends with a colon : character, and all the code that belongs to it is
indented. For example:

with open("a_file.txt", "w") as f:


for line in f:
print(line)

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.

When we perform any read/write operation, we may need to do :

• The operation at the current position of file pointer


• File pointer advances by the specified number of bytes.
Relative and Absolute Paths

• 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

Suppose current working directory is : delhi


We want to access sec_8.Xls file, then relative
address will be
..\NOIDA\SEC_8.XLS
Standard File Streams

• We use standard I/O Streams to get better performance from different I/O
devices.
• Some Standard Streams in python are as follows -

– Standard input Stream sys.stdin


– Standard output Stream sys.stdout
– Standard error Stream sys.stderr

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()

You might also like