You are on page 1of 14

Chapter 5 – Part 2

File Handling- Writing into text files


Class – XII
Subject: Computer Science
Writing Data Into Files
Till now we learned how to open a file and how
to read data from files, Now we will learn how
to write data into files using Python.

Why We need to write data in files--


1. To permanently save data of program
2. For future usage of data in the program
Procedure to write data in files –

1. Open The File in write mode ( w or a)


2. Write data using python functions(functions
will be discussed next).
3. Close the file
Functions for writing data
1. write()
Syntax : < file handle>. write(str1)
Use: It writes str1 to the file
2. writelines()
syntax: <filehandle>.writelines(L)
Use: It writes all strings in List L to the file
Example1 : Writing names of 5
students in a file using write() function
Details –
• Open a file STUDENT.TXT in write mode
• Get Names of 5 students from user using
input function in a loop
• Write each name using write() function
• Close the file
Example 2: Writing names of 5 students
in a file using writelines() function
Details –
• Open a file STUDENT.TXT in write mode
• Get Names of 5 students from user using
input function in a loop
• Append each name into a List L
• Write the list L in file using writelines()
• Close the file
Example3: Adding names of 5 students
into an existing file containing few names

• Open a file STUDENT.TXT in append mode


• Get Names of 5 students from user using
input function in a loop
• Write each name using write() function
• Close the file
• Close the file
Home Work
1. Write a program in python to read data from
one file ONE.TXT and write it in another file
TWO.TXT
2. Write a program in python to read data from
one file ONE.TXT and write only first 2 lines into
another file TWO.TXT
3. Write a program in python to read data from
one file ONE.TXT and Write only lines starting
with letter ‘T’ to another file TWO.TXT
Path: Sequence of directory(folder) names to access and particular file or
directory.
For Example: E:\Users\Public\Desktop\Files\myfile.docx

Absolute Path: The path of a file or directory from the top level of directory
structure.
For Example: E:\Users\Public\Desktop\Files\myfile.docx
Relative Path: The path of a file relative to current working directory which is
denoted as dot(.).
Notation for current working directory – dot(.)
Notation for parent directory – two dots(..)
For Example- If we are currently working in Desktop directory. Then the
relative path for file mentioned in previous example above will be –

.\Files\myfile.docx
1. Write a program to read data from a file
using absolute path of file.

2. Write a program to read data from a file


using relative file path
• Till now we studied only 3 file modes-
– r - Read Only
– w – Write Only
– a – Append
• Other file modes are –
– r+ : read and write mode
– Used for both read and write
– If file does not exist, error is raised
– w+ : write and read mode
– Used for both read and write
– If file does not exist, a blank file is created
– If file already exists, data is truncated
– a+ : append and read
– Used for both read and write
– If file does not exist, its created
– If file exists, data is not truncated
• File Pointer tells the current position in a file
where reading or writing will take place.
• File pointer moves forward as you read or
write data in a file
• Initial position of file pointer depends on file
mode-
– r, r+ - Beginning of file
– w,w+ - Beginning of file
– a,a+ - At the end of file
• seek()—seek() function is used to change the position of the file handle
(file pointer) to a given specific position. File pointer is like a cursor, which
defines from where the data has to be read or written in the file.
Usage–
f.seek(offset, from_where)
offset: Number of bytes to move the file pointer
from_where: Reference point for movement of file pointer-
• 0: sets the reference point at the beginning of the file, which is by default.
• 1: sets the reference point at the current file position.
• 2: sets the reference point at the end of the file.
• For example,
f.seek(10,1) from current position, move 10 bytes forward
f.seek(–20,1) from current position, move 20 bytes backward
f.seek(10,0) from beginning of file, move 10 bytes forward
• tell()—tell() returns the current position of the
file read/write pointer within the file. Its
syntax is:
f.tell()
• When you open a file in reading/writing
mode, the file pointer rests at 0th byte.
• When you open a file in append mode, the file
pointer rests at the last byte.

You might also like