File Handling in Python

Python File Handling

What is File?

The file is a collection of data used in almost all programming languages.

Why File Handling?

To handle large amounts of data to be stored in computer memory for future reference.

What are  Modes of File?

w = Write  / create file  

a = append / add to data to existing file

r = read / display information from file.

In python, both text and binary data can read and write.

The following are examples of File Handling in Python.

# File handling in python – Creating file and writing to file
file=open(‘D:\PythonExa\mytext.txt’, ‘w’)
file.write(“This is text is being inserted in text file”)
file.close()
print(“File created successfully!”)
OUTPUT:
File created successfully!
#Python File Handling – Storing numbers in File and Displaying using Loop. 
for x in range(11):
    with open(‘nump.txt’, ‘a’) as f:
        f.write(‘%d’ %x)
        print(x)
print(‘Done!’)
with open(‘nump.txt’, ‘r+’) as f:
    print(f.read())
OUTPUT:
0
1
2
3
4
5
6
7
8
9
10
Done!
012345678910
# File handling in python – Appending file 
file=open(‘D:\PythonExa\mytext.txt’, ‘a’)
file.write(” 12345678 Another line of text  is being inserted in text file”)
file.close()
print(“Data Added to File Successfully!”)
OUTPUT:
Data Added to File Successfully!
#File handling in python – Reading data from a file 
with open(“D:\PythonExa\mytext.txt”) as file:
    data = file.read()
# show data using print command
print(data)
OUTPUT:
This is text is being inserted in text file 12345678 Another line of text  is being inserted in the text file
# Program to show various ways to read and  write data in a file. 
file1 = open(“mtext.txt”,”w”)
string = [“This is Web \n”,”This is Data \n”,”This is AI \n”]
# \n is placed to indicate EOL (End of Line)
file1.write(“Hello \n”)
file1.writelines(string)
file1.close() #to change file access modes
file1 = open(“mtext.txt”,”r+”)
print (“Output of Read function is “)
print (file1.read())
print()
# seek(n) takes the file handle to the nth bite from the beginning.
file1.seek(0)
print (“Output of Readline function is “)
print (file1.readline())
print()
file1.seek(0)
# readlines function
print (“Output of Readlines function is “)
print (file1.readlines())
print()
file1.close()
OUTPUT:
Output of Read function is
Hello
This is Web
This is Data
This is AI
Output of Readline function is
Hello
Output of Readlines function is
[‘Hello \n’, ‘This is Web \n’, ‘This is Data \n’, ‘This is AI \n’]
#Store Name and Age using File handling in python – Appending file 
file=open(‘D:\PythonExa\std_det.txt’, ‘a’)
nm=input(‘Enter name:’)
ag=input(‘Age:’)
file.write(nm)
file.write(‘\t’)
file.write(ag)
file.close()
file=open(‘D:\PythonExa\std_det.txt’, ‘r+’)
print(file.readline(), end=’\n’)
file.close()
OUTPUT:
Enter name:Sharan
Age:21
shivaaa 21Ganesh 21Sharan 21
#Looping – printing series 
print(“Series – 1”)
for i in range(1, 5):
    for j in range(i):
         print(‘*’, end=’ ‘)
    print()
print(“Series – 2”)
for i in range(1, 5):
    for j in range(i):
         print(i, end=’ ‘)
    print()
print(“Series – 3”)
for i in range(1, 5):
    for j in range(i):
         print(i+j, end=’ ‘)
    print()
OUTPUT:
Series – 1
*
* *
* * *
* * * *
Series – 2
1
2 2
3 3 3
4 4 4 4
Series – 3
1
2 3
3 4 5
4 5 6 7

 

Share
Share
Scroll to Top