WRITING THE FILE
To write some text to a file, we need to open the file using the open method with one of the following
access modes.
w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.
a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file
exists.
SYNTAX:
f.write(“Messgae”);
Steps for Writing Data into a File in Python
1. Find the path of a file
We can read a file using both relative path and absolute path. The path is the location of the file on
the disk.
An absolute path contains the complete directory list required to locate the
file. A relative path contains the current directory and then the file name.
2. Open file in write mode
Pass file path and access mode w to the open() function. The access mode opens a file in write
mode.
For example, fp= open(r'File_Path', 'w')
3. Write content into the file
Once a file is opened in the write mode, write text or content into the file using the write()
method.
For example, fp.write('new text').
The write() method will add new text at the beginning of a file. For an existing file, this new
content will replace the existing content. If the file is not already present a new file will be created, and
content is written into it.
4. Close file after completing the write operation
File will be closed properly after completing the file operation. Use fp.close() to close a
file. 5. Append content at the end of the file
Pass file path and access mode a to the open() function to open a file in append mode.
For example, fp= open(r'File_Path', 'a')
Next, use the write() method to write content at the end of the file without deleting the existing
content.
READ FILE IN PYTHON
Temporary data that is locally used in a module will be stored in a variable. In large volumes of data, a
file is used such as text and CSV files and there are methods in Python to read or write data in those
files.
Steps for Reading a File in Python
1.Find the path of a file
We can read a file using both relative path and absolute path. The path is the location of the file on the
disk.
An absolute path contains the complete directory list required to locate the file.
A relative path contains the current directory and then the file name.
2.Open file in Read Mode
To open a file Pass file path and access mode to the open() function. The access mode specifies the
operation you wanted to perform on the file, such as reading or writing. For example, r is for
reading.
For example, fp= open(r'File_Path', 'r')
3. Read content from a file.
Once opened, we can read all the text or content of the file using the read() method. You can also use the
readline() to read file line by line or the readlines() to read all lines.
For example, content = fp.read()
4. Close file after completing the read operation
We need to make sure that the file will be closed properly after completing the file operation. Use
fp.close() to close a file.
File Read Methods
Method Description
read() Returns the entire file content and it accepts the optional size parameter that mentions
the bytes to read from the file.
readline The readline() method reads a single line from a file at a time. . Accepts optional size
()
Method Description
parameter that mentions how many bytes to return from the file.
readlines()The readlines() method returns a list of lines from the file
PROGRAM 1
Write a program to store the content into a file.
fp=open("file1.txt","w")
fp.write("Sri Eshwar College of Engineering")
fp=open("file1.txt","r")
print(fp.read(3))
fp.close()
OUTPUT
PROGRAM 2
Write a program to read the content from file
fp=open("file1.txt","w")
fp.write("Sri Eshwar College of Engineering \n Coimbatore ")
fp=open("file1.txt","r")
print(fp.readline())
print(fp.readline())
fp.close()
OUTPUT
PROGRAM 3
Write a program to read the content from file using readlines().
fp=open("file1.txt","w")
fp.write("Sri Eshwar College of Engineering \n Coimbatore ")
fp=open("file1.txt","r")
print(fp.readlines())
fp.close()
OUTPUT
CREATING A NEW FILE
The new file can be created by using one of the following access modes with the function open().
x: it creates a new file with the specified name. It causes an error a file exists with the same
name.
a: It creates a new file with the specified name if no such file exists. It appends the content to the file if
the file already exists with the specified name.
w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.
PROGRAM 4
Write a program check whether new file is created .
fileptr = open("file1.txt","x")
print(fileptr)
if fileptr:
print("File created successfully")
OUTPUT
PROGRAM 5
fileptr = open("sece.txt","x")
print(fileptr)
if fileptr:
print("File created successfully")
OUTPUT
THE WITH STATEMENT
The with statement was introduced in python 2.5. The with statement is useful in the case of manipulating
the files. It is used in the scenario where a pair of statements is to be executed with a block of code in
between.
The syntax to open a file using with the statement is given below.
with open(<file name>, <access mode>) as <file-pointer>:
PROGRAM 6
Write a program to read file content using with
with open("file1.txt",'r') as f:
content = f.read();
print(content)
OUTPUT
READ FILE THROUGH FOR LOOP
PROGRAM 7
Write a program to read content from file using for
loop fileptr = open("file1.txt","r");
#running a for loop
for i in fileptr:
print(i) # i contains each line of the file
OUTPUT