Python Tutorial | Python Data File Handling 3

Writing data to a File

We can write character data into a file in Python by using the following two methods.
1. write(string)
2. writelines(sequence of lines)

write():

write() method takes a string as a parameter and writes in the file. for storing data with the end of line character, we will have to add ‘\n’ character to the end of the string.
An argument to the function has to be string, for storing numeric value, we have to convert it to a string.

Syntax:
fileobject.write(string)

writelines():

for writing a string at a time we use write() method. It cannot be used to write a list, tuple, etc into a file. Sequence data type including strings can be written using writelines() method in the file.

Syntax:
fileobject.writelines(sequence)

So whenever we have to write a sequence of string/data types, we must use writelines instead of write().

Also to be noted here is that writelines() method does not add any EOL (end of the line) character to the end of the string. We have to do it ourselves. So to resolve this problem we have to use ‘\n’ (newline character) after the end of each list item or string.

Example of write() method:

Python program to write contents into a file named “abc.txt”

#to wite contents in the file
f=open("abc.txt","w")
f.write("This is file abc.txt\n")
f.write("Hello how are you\n")
f.write("Example of write method\n")
f.write("end of file\n")
#close the file
f.close()
print("File created")

Now to read the contents of the above created file and display them.

#to read the contents of the file
f=open("abc.txt","r")
data=f.readlines()
for i in data:
    print(i,end='')
    
#close the file
f.close()

Output:

This is file abc.txt
Hello how are you
Example of write method
end of file
>>> 

Python program to write roll,name and per in the file named “abc1.txt”

#to write contents in the file
f=open("abc1.txt","w")
roll=101
f.write("Amit\n")
f.write(str(roll))
per=98.99
f.write("\n")
f.write(str(per))
#close the file
f.close()

Now to read the contents of the above created file and display them.

#to read the contents of the file
f=open("abc1.txt","r")
data=f.readlines()
for i in data:
    print(i,end='')
    
#close the file
f.close()

Output:

Amit
101
98.99
>>> 

Example of writelines()

Python program to write list sequence (names) in the file “abc.txt”

#to write contents in the file
f=open("abc.txt","w")
names=["Amit\n","Sumit\n","Rohit\n","Kapil\n","Neha\n"]
f.writelines(names)
#close the file
f.close()

Now to read the contents of the above created file and display them.

#to read the contents of the file
f=open("abc.txt","r")
data=f.readlines()
for i in data:
    print(i,end='')
    
#close the file
f.close()

Output:

Amit
Sumit
Rohit
Kapil
Neha
>>> 

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Visitor Counter

0051049