Data File Handling Text File
Data File Handling Text File
CITY CAMPUS,
DALANWALA, DEHRADUN
With : Apart from using open() function for creation of file, with statement
can also be used for the same purpose. We can use this statement to group
file operation statements within block to make the code compact and much
more readable. We are required to close the file explicitly using wit
statement.
The os module provides functions for working with files and directories OS
stand for operating system.
imprt os
ns=os.cwd()
print(ns)
1
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
seek() method
In Python, seek() function is used to change the position of the File
Handle to a given specific position. File handle is like a cursor, which defines
from where the data has to be read or written in the file.
The reference point is selected by the from_what argument. It accepts three
values:
Note: Reference point at current position / end of file cannot be set in text
mode except when offset is equal to 0.
tell():The tell() method returns the current file position in a file stream.
2
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
Access
Modes Description
r Opens a file for reading only.
rb Opens a file for reading only in binary format.
r+ Opens a file for both reading and writing.
rb+ Opens a file for both reading and writing in binary format.
w Opens a file for writing only.
wb Opens a file for writing only in binary format.
w+ Opens a file for both writing and reading.
wb+ Opens a file for both writing and reading in binary format.
a Opens a file for appending.
ab Opens a file for appending in binary format.
a+ Opens a file for both appending and reading.
ab+ Opens a file for both appending and reading in binary format.
3
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
Find answer below for data file handling in python class 12.
Answers:
1. File
2. File Handling
3. I/O Operations
4. Data file
5. Text File
6. open(“data.txt”,”r”)
7. open(“data.txt”,”w”)
8. File handle or File Object
9. close
10. read(15)
11. readline()
12. readlines()
13. write()
14. writelines()
15. append
16. flush()
4
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
Write a method in python to read the content from a text file diary.txt line by line and
display the same on screen.
Ans.
def read_file():
ns = open('diary.txt', 'r')
for line in ns:
print (line)
ns.close()
Observe the following code and answer the questions that follow: File =
open("Mydata","a") _____________________ #Blank1 File.close() i.
What type (Text/Binary) of file is Mydata? ii. Fill the Blank 1 with
statement to write “ABC” in the file “Mydata”
Ans.
i. Text File
ii. ii. File.write("ABC")
5
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
Write a function countmy( )in Python to read the text file “DATA.TXT” and
count the number of times “my” occurs in the file. For example if the file
“DATA.TXT” contains: “This is my website. I have displayed my
preferences in the CHOICE section.”
The countmy( ) function should display the output as: “my occurs 2
times”.
def countmy():
ns=open("data.txt","r")
a=ns.read()
b=a.split()
count=0
for i in b:
if i=="my":
count=count+1
print("my in the file=",count)
ns.close()
Differentiate between “w” and “r” file modes used in Python while opening
a data file. Illustrate the difference using suitable examples
A file is opened using “w” mode to write content into the file. A file is
opened using “r” mode to read content into the file. Example:
def Create():
file=open('NOTES.TXT','w')
S="This is a sample" file.write(S)
file.close()
def Read():
file=open('NOTES.TXT','r')
Lines=file.readline();
print(Lines)
file.close()
Create();
Read();
6
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN
for L in Lines:
print(L.upper())
file.close()
word = line.split()
for w in word:
if w=="IS" or w=="TO" or w=="UP":
c=c+1
print( "Count of IS TO and UP is ",c)
file.close()
9
NARENDRA SRIVASTAVA , DOON INTERNATIONAL SCHOOL DEHRADUN