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

File Handling Text+Binary+CSV

Here are functions to solve the practice questions: 1. def search_writeT(): f1 = open('abc.txt','r') f2 = open('xyz.txt','w') for line in f1.readlines(): if line[0]=='T': f2.write(line) f1.close() f2.close() 2. def display_numbers(): f = open('file.txt','r') for line in f.readlines(): for char in line: if char.isdigit(): print(char) f.close() 3. def remove_lowercase(file1, file2): f1 = open

Uploaded by

Prateek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

File Handling Text+Binary+CSV

Here are functions to solve the practice questions: 1. def search_writeT(): f1 = open('abc.txt','r') f2 = open('xyz.txt','w') for line in f1.readlines(): if line[0]=='T': f2.write(line) f1.close() f2.close() 2. def display_numbers(): f = open('file.txt','r') for line in f.readlines(): for char in line: if char.isdigit(): print(char) f.close() 3. def remove_lowercase(file1, file2): f1 = open

Uploaded by

Prateek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Made by Saurabh Arora

Need for a data file


❖ To Store data in organized manner
❖ To store data permanently
❖ To access data faster
❖ To Search data faster
❖ To easily modify data later on

Made by Saurabh Arora


Text File
Myfile.txt

A file whose contents can be viewed using a text


editor is called a text file. A text file is simply a
sequence of ASCII or Unicode characters.
Example: .txt files

In python , Each line of text is terminated with a


special EOL (End of Line) character which is \n
(new line) character.

Made by Saurabh Arora


File Modes in a Text File
1. Read Mode (r) – It is used for reading the files.

2. Write Mode (w) – It is used for writing of files.

3. Append Mode (a)– It is used to append the


data in the file. append means to add data in
the ending of file.

Made by Saurabh Arora


Opening of files
Files can be opened using open function

f=open(“Myfile.txt”)
f is known as file
or
object, file handle
f=open(“Myfile.txt”,“r”) or file variable

❖ In the above line a text file “Myfile.txt” is opened in read


mode.
❖ If no file mode is specified then by default read mode is
used.
❖ A file variable is needed to read or write the files in python
❖ if Myfile.txt does not exist then it will throw error.
Made by Saurabh Arora
Closing of files
Files can be closed using close function

f is known as file
object, file handle
or file variable
f.close()

❖ In the above line a text file “Myfile.txt” is closed


❖ The syntax for closing the file is always same and does
not depend on the mode in which the file was opened.
❖ After using close() reading and writing is not possible.
Made by Saurabh Arora
Extra Slide for Important Info.

Raw String treats \ and n as different characters


as given in the example below

Here, r is raw string


Raw String treats \ and n
as different characters as
>>> s=r"Hi\nHello" given in the example of
>>> print(s) string s.
Hi\nHello
>>> len(s)
9
>>> t="Hi\nHello"
>>> print(t)
Hi
Hello
>>> len(t)
In string t, \n is a
8 single character
representing enter

Made by Saurabh Arora


Opening files in different directories (2 ways)
Here, r is raw string

>>>f=open(r"C:\Users\Saurabh\Desktop\Homework.txt", "r")
>>> f.close()
>>> f=open(r"D:\India.txt", "w")
>>> f.write("Hello")
5
>>> f.close()

>>> f=open("C:\\Users\\Saurabh\\Desktop\\Homework.txt", "r")


>>> f.close()
>>> f=open("D:\\India.txt","r")
>>> f.close()

Made by Saurabh Arora


Reading from files
Different functions that can be used for reading the files are-

1. read function -

The read() method reads the entire file and returns its
contents in the form of a string.

read(n) – It will read n characters from file.


If size is mentioned in the arguments of read function like
read(10) then it will read 10 bytes (10 characters) from the file.

Made by Saurabh Arora


Reading from files
read function –

>>> f=open("abc.txt","r")
>>> s=f.read()

>>> print(s)
The weather is very good
Today is Monday
Month is April
Year is 2020
t gave empty string because
the file pointer is at the end
>>> t=f.read(10) and the file is already read
>>> print(t) by read() function.

>>> f.close()
Made by Saurabh Arora
Reading from files
read function –

>>> f=open("abc.txt","r")
>>> a=f.read(15)
>>> a
'The weather is '

>>> b=f.read(20)
>>> b Note: \n is treated as 1 character
'very good\nToday is M'
Printing c variable by using print()
>>> c=f.read(25) >>> print(c)
onday
>>> c
Month is April
'onday\nMonth is April\nYear‘
Year'
>>> f.close()
Made by Saurabh Arora
Reading from files
2. readline function -
The readline() method reads only a single line from the file
and returns its contents in the form of a string.

>>> f=open("abc.txt","r")
>>> p=f.readline()
>>> p
'The weather is very good\n'
>>> q=f.readline()
>>> q
'Today is Monday\n'
>>> print(q)
Today is Monday

>>>
Made by Saurabh Arora
Reading from files
3. readlines function -
The readlines() method reads all the lines from the file and
returns its contents in the form of a list of string elements.

>>> f=open("abc.txt","r")
>>> L=f.readlines()
>>> L
['The weather is very good\n', 'Today is Monday\n', 'Month is April\n', 'Year is 2020']
>>> print(L)
['The weather is very good\n', 'Today is Monday\n', 'Month is April\n', 'Year is 2020']

Made by Saurabh Arora


Reading from files
3. readlines function (continued) -
>>> print(L)
['The weather is very good\n', 'Today is Monday\n', 'Month is April\n', 'Year is 2020']

>>> for i in L:
print(i)

The weather is very good

Today is Monday

Month is April

Year is 2020
>>>
Made by Saurabh Arora
Split Function
Split function is applied on strings to break it into many parts. The
output is list of string elements.

>>> s="Today is Monday" If no input is given in split


>>> A=s.split() function then by default it
splits with “ ” (space)
>>> print(A)
['Today', 'is', 'Monday']

>>> t="Today is Monday"


>>> B=t.split(" ")
>>> B
['Today', 'is', 'Monday']

>>> u="aman:101:12A"
>>> C=u.split(":")
>>> C
['aman', '101', '12A']

Made by Saurabh Arora


Split Function

>>> y="Today is Monday\nDate is 04-05-2020"


>>> print(y)
Today is Monday Special case: If string contains both
“ ”(space) and \n characters then
Date is 04-05-2020 split function splits with both “ ”
(space) and \n only when no input
>>> L=y.split() is given to it.

>>> print(L)
['Today', 'is', 'Monday', 'Date', 'is', '04-05-2020']
>>>

Made by Saurabh Arora


Writing in files
For writing, the file should be opened in write mode
f=open(“Myfile.txt”,“w”)

Different functions that can be used for writing files are-


1. write function -

The write(s) method is used to write or insert the string s in a single


line in the text file

❖ The text file “Myfile.txt” should be opened in write mode.


❖ If no such file exists, then it will be created automatically.
❖ If the file already exists, then it will be overwritten.
❖ Most Important thing is to use close function at the end because
f.close() will save the file opened in write mode, otherwise the file
will not be saved.
Made by Saurabh Arora
Writing in files
1. write function -

>>> f=open("xyz.txt","w")
>>> s="Today is Thursday"
>>> f.write(s)
17
>>> t="The Date is 30-04-2020"
>>> f.write(t)
22
>>> f.close()
>>>

Made by Saurabh Arora


Writing in files
2. Writelines(L) function - For a list of string elements, each string
in the list is inserted in the text file. It is used to insert multiple
strings at a single time.

>>> f=open("xyz.txt","w")
>>> L=["Computer Science Class\n","Today is Thursday\n","The Date is 30-04-2020"]
>>> f.writelines(L)
>>> f.close()
>>>

Made by Saurabh Arora


Append data in files
Suppose there is a file myfile.txt

And when we have to append (add) some data inside this


file, then append mode (a) should be used

Made by Saurabh Arora


Append data in files
>>> f=open("myfile.txt","a")
>>> f.write("\n")
1
>>> f.write("Ravi Pandey")
11
>>> f.close()
>>>

Made by Saurabh Arora


Note:

1. read() is used when you have to work on digits, characters,


words (words with the help of split function only)
2. readlines() is used when we are working on lines

Made by Saurabh Arora


Practice Questions

Question 1 : Write a function search_writeT() in python to read the contents


from the file “abc.txt” and then write those lines which start with the letter
‘T’ to the file “xyz.txt”

Made by Saurabh Arora


Practice Questions

Question 2 : Write a function in python that reads a file and display all the
numbers or digits present in the file

Question3. Write a function remove_lowercase() that accepts two filenames


and copies all the lines that do not start with lowercase letter from the first
file into the second file.

Question4. Write a function fcombine() that accepts three filenames and


combines the content of two files into the third file one after the other.

Made by Saurabh Arora


Ques. Write a function findlinesT(filename) in python to display those lines in
the program which start with the letter ‘T’

def findlinesT(filename):
f=open(filename,"r")
L=f.readlines()
for i in L:
if i[0]=='T':
print(i)

findlinesT("abc.txt")

Output:
Today is Monday

The weather is very good


Made by Saurabh Arora
Ques. A text file salary.txt contains empid, name and salary in the following format
empid:name:salary. Write a function print_sal() to print the salary of that employee
whose name is taken as input from the user
def print_sal():
name=input("Enter the name-")
f=open("salary.txt","r")
L=f.readlines()
print(L)
for i in L:
A=i.split(':')
print(A)
if A[1]==name:
print("Salary is ",A[2])
print_sal()

Output:
Enter the name-harsh
['101:arjun:15000\n', '102:sonu:10000\n', '103:ram:25000\n', '104:harsh:20000']
['101', 'arjun', '15000\n']
['102', 'sonu', '10000\n']
['103', 'ram', '25000\n']
['104', 'harsh', '20000']
Salary is 20000 Made by Saurabh Arora
Ques. Write a function count_is() for counting the number of times “is” word occurs
inside the file

def count_is(str1):
f=open(str1,"r")
s=f.read()
L=s.split()
c=0
for i in L:
if i=="is":
c=c+1
print("Number of is = ",c)
f.close()

count_is("abc.txt")

Output:

Number of is = 4

Made by Saurabh Arora


Ques. Write a function count_alphabets() for counting the number of alphabets
written inside the file.

def count_alphabets():
f=open("abc.txt","r")
s=f.read()
c=0
for i in s:
if i.isalpha():
c=c+1
print("Number of alphabets = ",c)
f.close()

count_alphabets()

Output:

Number of alphabets = 51

Made by Saurabh Arora


Ques. Write a function findmax() that accepts a filename and prints longest line in
the file.

def findmax(filename):
f=open(filename,"r")
L=f.readlines()
big=L[0]
for i in L:
if len(big)<len(i):
big=i
print("Biggest line is ",big)
findmax("abc.txt")

Output:
Biggest line is The weather is very good

Made by Saurabh Arora


Ques. Write a function remove_lowercase() that accepts two filenames and copies all
the lines that do not start with lowercase letter from the first file into the second file.

def remove_lowercase(filename1,filename2):
fr=open(filename1,"r")
fw=open(filename2,"w")
L=fr.readlines()
for i in L:
if not i[0].islower():
fw.write(i)
fr.close()
fw.close()
remove_lowercase("ex1.txt","ex2.txt")

Output:

Made by Saurabh Arora


flush() Function
When we write data on a file using write functions, python holds
everything to write on file in a buffer (memory) and saves it into the
actual file after you write close() function.

If we use flush() function the contents stored in the buffer (memory) is


written on the file.

>>> f=open("aug25.txt","w")
>>> f.write("Ravi Pandey")
11
>>> f.write("\nAman Kumar Jha")
15
>>> f.flush()
>>> f.write("\nShubham Kumar")
The data gets
14
written and saved
>>> f.flush()
into the file.
>>> f.close()

Made by Saurabh Arora


Removing Characters from Starting and
Ending of Lines
❖ strip() removes the given character from both ends
❖ rstrip() removes the given character from last i.e. right end
❖ lstrip() removes the given character from starting i.e. left

>>> f=open("aug25.txt","r")
>>> s=f.readline()
>>> s
'Ravi Pandey\n'
>>> print(s)
Ravi Pandey

>>> s=s.rstrip('\n')
>>> s
'Ravi Pandey'

Made by Saurabh Arora


Standard Input, Output and Error Streams
Standard input device (stdin) – Keyboard Streams means
Standard output device (stdout)– Monitor sequence of
data
Standard error device (stderr)– Monitor

The standard input, output and error device can also be used as
files by just importing sys module.

sys.stdin.read() can be used to read data from keyboard.


sys.stdout.write(“Hello”) and sys.stderr.write(“Hello”) can be used
for displaying data on monitor.

Made by Saurabh Arora


Python files : with statement

>>>with open (“output.txt”, “w”) as f:


f.write(“HI HELLO”) File automatically closed as
you come out of the
>>> indentation code.

The with statement automatically closes the file after the block of
code gets over. So there is no need here to write f.close()

If an error occurs before the end of the block, the with statement
will handle it and will automatically close the file.

Made by Saurabh Arora


Absolute and Relative Paths
Path is a sequence of directories(folders) which gives you
hierarchy to access a particular folder or file.
Example: C:\Computer Science\Python\abc.py

A Relative path starts from the current working directory


whereas Absolute path starts from the topmost directory of
file system.

Example of Absolute Path –


f=open("C:\\Users\\Saurabh\\Desktop\\Homework.txt", "r")

Example of Relative Path -


f=open("Homework.txt", "r")

Made by Saurabh Arora


Finding Current Working Directory of Python

Current Working Directory is


different for each version of
python. It is the folder where all
the python programs are stored.

Made by Saurabh Arora


Binary Files
A binary file contains information in the same format as it is
held in memory. Binary files can be any sequence of bytes.

Binary files are easier and faster to read and write than text
files. In binary files there is no delimiter to a line. (‘\n’ was
used in text files for ending the line.)

Binary files are any files where the format isn't made up of
readable characters. Binary files can range from image files
like JPEGs or GIFs, audio files like MP3s or binary document
formats like Word or PDF.

In Python, files are opened in text mode by default. To open


files in binary mode, when specifying a mode, add 'b' to it.

Made by Saurabh Arora


File Modes in a Binary File
1. Read Mode (rb) – It is used for reading the
files.

2. Write Mode (wb) – It is used for writing of


files.

3. Append Mode (ab)– It is used to append the


data in the file. append means to add data in
the ending of file.

Made by Saurabh Arora


Pickling (Serialization) is the process in which a python structure is
converted into byte stream for writing it to the file. dump() function
is used for pickling.

Unpickling (De-Serialization) is the process of converting the byte


stream back to the original structure. load() function is used for
unpickling

❖ Structure can be your List, Tuple, Dictionary or Class Object.

It is done with the help of pickle module.


Both dump() and load() functions can be used after importing pickle
module

dump() function is used to write object to the binary file


load () function is used to read object from the binary file

Made by Saurabh Arora


Examples of Reading and Writing Objects in Binary Files

>>> import pickle


>>> L=[10,20,30,40,50]
>>> f=open("sep1.dat","wb")
>>> pickle.dump(L,f)
>>> f.close()
>>> fr=open("sep1.dat","rb")
>>> A=pickle.load(fr)
>>> print(A)
[10, 20, 30, 40, 50]

Made by Saurabh Arora


>>> import pickle
>>> A={1:"shubham",2:"sagar",3:"vinay"}
>>> B={4:"aman",5:"ravi",6:"vishal"}
>>> f=open(“sep2.dat","wb")
>>> pickle.dump(A,f)
>>> pickle.dump(B,f)
>>> f.close()
>>> fr=open(“sep2.dat","rb")
>>> X=pickle.load(fr)
>>> Y=pickle.load(fr)
>>> X
{1: 'shubham', 2: 'sagar', 3: 'vinay'}
>>> Y
{4: 'aman', 5: 'ravi', 6: 'vishal'}
>>>
Made by Saurabh Arora
try except in python
The try block lets you test a block of code for errors.

The except block lets you handle the error.

try:
print(x)
except:
print("An error occurred")

The above code should give error as variable x does not exist, but try except helps to handle this
error means the error does not occurs.

Since the try block raises an error, the except block will be executed. Without the try block, the
program will crash and raise an error:

The above code gives the following output:

An error occurred

Made by Saurabh Arora


Reading Binary Files Function

EOFError means End of File


Error, it occurs when user is
trying to read objects from
binary file but there is no object
left to read because all objects
have already been read.
Made by Saurabh Arora
Writing Binary Files Function
Example : Writing objects (Dictionary)

The append mode ab is used because


we want to preserve previous contents
here and after using write mode wb
the previous contents will be lost.
Made by Saurabh Arora
Searching Binary Files Function
Example : searching object (Dictionary) according to roll number

Made by Saurabh Arora


Deleting Records Binary Files Function
Example : deleting object (Dictionary) according to roll number

Made by Saurabh Arora


Updating Records Binary Files Function
Example : updating object (Dictionary) marks value according to roll number

Made by
Saurabh Arora
seek() and tell() function
seek() function changes the position of the file pointer by placing
the file pointer at a specific position in the file.

f.seek (location)
f.seek (location, from_what)

from_what argument. It accepts three values:


 0: sets the reference point at the beginning of the file
 1: sets the reference point at the current file position
 2: sets the reference point at the end of the file

The from_what argument takes value 1 & 2 only in case of binary


files.

tell() function gives us the current position of file pointer within


the file.

Made by Saurabh Arora


Giving negative value
to seek function in the
location always gives
error

Made by Saurabh Arora


Files modes continued…….
 r opens a text file for reading only. If the file does not exist,
then error occurs

 rb opens a binary file for reading only. If the file does not exist,
then error occurs

 r+ opens a text file for both reading and writing. If the file does
not exist, then error occurs

 rb+ or r+b opens a binary file for both reading and writing. If
the file does not exist, then error occurs

Made by Saurabh Arora


Files modes continued…….
 w opens a text file for writing only. Overwrites the file if the
file exists. If the file does not exist, creates a new file for
writing.

 wb opens a binary file for writing only. Overwrites the file if


the file exists. If the file does not exist, creates a new file for
writing.

 w+ opens a text 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 writing and reading.

 wb+ or w+b opens a binary 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 writing and reading.

Made by Saurabh Arora


Files modes continued…….
 a opens a text file in append mode. If file exists, data is
retained and new data being written will append to the end. If
the file does not exist, creates a new file for writing.

 ab opens a binary file in append mode. If file exists, data is


retained and new data being written will append to the end. If
the file does not exist, creates a new file for writing.

 a+ opens a text file for both writing and reading. If file exists,
data is retained and new data being written will append to the
end. If the file does not exist, creates a new file for writing and
reading.

 ab+ or a+b opens a binary file for both writing and reading. If
file exists, data is retained and new data being written will
append to the end. If the file does not exist, creates a new file
for reading and writing.

Made by Saurabh Arora


CSV Files
CSV stands for comma separated values. They are delimited files
that store tabular data (data is stored in rows and columns as in
spreadsheets or databases). CSV files are normally created by
programs that handle large amounts of data. They are a
convenient way to export data from spreadsheets and databases
as well as import or use it in other programs.

The comma delimits every value means the values are separated
with comma. The default delimiter is comma but newer versions
can have different delimiter character other than comma

Each line in a csv file is a data record. Each record consists of one
or more fields, separated by comma.

Made by Saurabh Arora


csv module is used for reading and writing of data in csv files.

 csv.reader
It is used for reading from CSV file. The csv.reader() function
returns a reader object and each row returned by the reader object
is a list of String elements

 csv.writer
It is used for writing to CSV file. The csv.writer() function
returns a writer object that can be used to write into CSV files
using the writerow(), writerows() function

Made by Saurabh Arora


Write row by row in CSV Files

import csv
fw=open("student.csv","w")
w=csv.writer(fw)
w.writerow(['a','b','c'])
w.writerow(['x','y','z','w'])
w.writerow(['m','n','o','p'])
fw.close()

Made by Saurabh Arora


Write multiple rows in CSV Files

import csv
fw=open("student2.csv","w")
L=[['a','b','c'],['d','e','f'],['g','h','i'],['x','y','z']]
w=csv.writer(fw)
w.writerows(L)
fw.close()

Made by Saurabh Arora


Reading contents from CSV Files

import csv
fr=open("student.csv","r")
cr=csv.reader(fr)
for i in cr:
print(i)

Output:
['a', 'b', 'c'] The reason for the
empty lists are the
[] empty rows in CSV
['x', 'y', 'z', 'w'] file. The problem
can be solved by
[] using the newline
argument in open
['m', 'n', 'o', 'p']
function while
[] writing this CSV
File.

Made by Saurabh Arora


Write row by row in CSV Files using newline argument

import csv
fw=open("student.csv","w",newline="")
w=csv.writer(fw)
w.writerow(['a','b','c'])
w.writerow(['x','y','z','w'])
w.writerow(['m','n','o','p'])
fw.close()

There is no empty row in csv file


because we have used
newline="" argument in open
function while writing this CSV
File. The default value of this
newline argument is \r\n

Made by Saurabh Arora


Reading contents from CSV Files

import csv
fr=open("student.csv","r")
cr=csv.reader(fr)
for i in cr:
print(i)

Output:
['a', 'b', 'c']
['x', 'y', 'z', 'w']
['m', 'n', 'o', 'p']

No empty List came in output


as no there is no empty row in
csv file because we had used
newline argument in open
function while writing this
CSV File.

Made by Saurabh Arora


Ques. Creating a CSV File storing students information,
also appending CSV File

import csv
f=open("oct1.csv","w",newline="\r\n")
cw=csv.writer(f)
cw.writerow(['RollNo','Name','Marks'])
cw.writerow(['1','sanjay','60'])
cw.writerow(['2','abhimanyu','70'])
cw.writerow(['3','ravi','80'])
cw.writerow(['4','aman','90'])
f.close()

f=open("oct1.csv","a",newline="\r\n")
cw=csv.writer(f)
cw.writerow([5,'saket',100])
f.close()
Made by Saurabh Arora
Ques. Find the Number of Records in a CSV File

import csv
f=open("oct1.csv","r",newline="\r\n")
cr=csv.reader(f)
n=0
for i in cr:
if cr.line_num==1:
continue
n=n+1
print(n)

Output:
5

Made by Saurabh Arora


Ques. Program to search a record for a student in CSV
File whose name is entered by the user
import csv
name=input("Enter the name to display record - ")
f=open("oct1.csv","r",newline="\r\n")
cr=csv.reader(f)
for row in cr:
if name==row[1]:
print(row)
f.close()

Output:
Enter the name to display record - aman
['4', 'aman', '90']

Made by Saurabh Arora

You might also like