Python Core



Admission Enquiry Form


File Handling in python

The file handling plays very important role in python if we need to be store data permanently into the file. A file is a named location on hard disk to store the information. We can also access the stored information when we require.

File Operations in Python

  1. Opening a file
  2. Writing or reading or append of file
  3. Closing a file

Opening a file using open() method

Opening of a file means open a file before performing any task on it such as writing, reading etc. Python provides an open() function that accepts two arguments, file name and access mode in which the file is accessed.An open() function returns a file object which can be used to perform different operations like reading, writing etc.

The close() method

When all the operations are done into the file, we must close it using the close() method given in python.

Q1. Program to write a string into the file

file=open("first.txt","w")
str="Welcome to Compuhelp."
file.write(str)
print("Data saved")
file.close()




Q2. Program to write an integer into the file

file=open("first.txt","w")
num=input("Enter a number")
file.write(num)
print("Data saved")
file.close()




Q3. Program to write and read an integer to the file

file=open("first.txt","w")
num=input("Enter a number")
file.write(num)
print("Data saved")
file.close()
file=open("first.txt","r")
n=file.read()
print("Value of n is : ",n)
file.close()




Q4. Program to write a string and read it using for loop

file=open("first.txt","w")
file.write("Welcome to Compuhelp")
file.close()
file=open("first.txt","r")
for i in file:
    print(i)




Q5. Program to write a string, read it from file and find its length

file=open("first.txt","w")
file.write("Welcome to Compuhelp")
file.close()
file=open("first.txt","r")
str=file.read()
length=len(str)
print(length)




Q6. Program to write a string, read it from file and count vowels in it.

vowels='aeiouAEIOU'
file=open("first.txt","w")
file.write("Compuhelp Where Teaching Is Passion")
file.close()
file=open("first.txt","r")
str=file.read()
count=0
for s in str:
if s in vowels:
count=count+1
print("Vowels are :",count)




Q7. Program to write a string, read it from file and count UPPERCASE & lowercase characters in it.

lowercase="abcdefghijklmnopqrstuvwxyz"
uppercase="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
file=open("first.txt","w")
file.write("Compuhelp Pvt Ltd")
file.close()
file=open("first.txt","r")
str=file.read()
count=0
counte=0
for s in str:
if s in lowercase:
count=count+1
if s in uppercase:
counte=counte+1
print("Lower case letter",count)
print("Upper case letter",counte)




Q8. Program to write a string, read it from file and count number of words & spaces in it.

file=open("first.txt","w")
file.write("Compuhelp Computer Education")
file.close()
file=open("first.txt","r")
str=file.read()
space=0
wrd=0
for i in str:
if(i==' '):
space=space+1
wrd=wrd+1
print("Number of spaces",space)
print("Number of words",wrd+1)




Q9. Program to write string, read it from file and count number of lines.

file=open("first.txt","w")
file.write("1st line in File\n")
file.write("2nd line in file\n")
file.write("3rd line in file\n")
file.write("4th line in file")
file.close()
file=open("first.txt","r")
str=file.read()
count=1
for i in str:
if i=="\n":
count=count+1
print("Total lines in file:",count)




Q10. Program to write ten number into file, read them and find odd & even numbers.

file=open("first.txt","w")
for i in range(1,11):
num=input("Enter number")
file.write(num)
file.write("\n")
file.close()
file=open("first.txt","r")
for i in file:
j=int(i)
if j%2==0:
print(j,end=" ")
print("\n")
file.seek(0)
for i in file:
j = int(i)
if j%2!=0:
print(j,end=" ")




Q11. Program to write a string,read it and convert a character to UPPERCASE entered by user.

file=open("first.txt","w")
i=input("Enter text")
file.write(i)
file.close()
case=input("Enter alphabet to be converted")
file=open("first.txt","a+")
file.seek(0)
str=file.read()
file.close()
file=open("first.txt","w")
for i in str:
if(i==case):
i=i.upper()
print(i,end="")
file.write(i)
else:
print(i,end="")
file.write(i)




Q12. Program to write and read data from file using classes and functions.

#WAP to write and read data from file using classes and functions
class a:
def input(self):
myfile=open("first.txt","w")
myfile.write("Hello")
myfile.close()
def display(self):
myfile=open("first.txt","r")
print(myfile.read())
myfile.close()
obj=a()
obj.input()
obj.display()




Q13. Program to write and read data from file using classes and functions by taking input from the user.

#WAP for user to store data and read data from file using classes and objects
class a:
def input(self):
myfile=open("first.txt","a+")
self.str=input("Enter a string to enter in file")
myfile.write(self.str)
myfile.close()
def display(self):
myfile=open("first.txt","r")
print(myfile.read())
obj=a()
i=0
while i==0:
n = int(input("to insert press 0 to display press 1"))
if n==0:
obj.input()
elif n==1:
obj.display()
else:
print("Wrong input")




Q14. Program to take a list as input from user and add that list into the file.

#WAP to take a list from user and add that list into the file
mylist=[]
n=int(input("Enter the size of list"))
for x in range(0,n):
    y=input("Enter list item")
mylist.append(y)
print(mylist)

myfile=open("first.txt","w")
for l in mylist:
str1=l
z=str(str1)
myfile.write(z)

myfile.close()




Q15. Program to store a list into the file.

#WAP to store a list in file
mylist=['Hello',10,20,30]
file=open("first.txt","a+")
for iin range(0,4):
t=mylist[i]
l=str(t)
file.write(l)




Q16. Program to read data from a file and append data in that file.

#WAP to read data from a file and append data in that file
myfile=open("first.txt","r")
print(myfile.read())
myfile.close()
myfile=open("first.txt","a+")
myfile.write("Text appended")
myfile.seek(0)
print(myfile.read())




Q17. Program to take input from user and enter that into file and read that data.

#WAP to take input from user and enter that into file and read that data
str1=input("Enter 1st string")
str2=input("Enter 2nd string")
myfile=open("first.txt","a+")
myfile.write(str1)
myfile.write(str2)
myfile.close()
myfile=open("first.txt","r")
str3=myfile.read()
print(str3)




Python Command Line Arguments.

import sys
length=len(sys.argv)
print("The length of the command line arguments is :",length)
print("Printing the arguments of the command line")
for x in sys.argv:
print(x)

#Now compile this code from command line and pass arguments
C:\Users\hp>python C:/Users/hp/PycharmProjects/pythonProject/DemoCmdArgs.py first second third 100

OUTPUT:

The length of the command line arguments is : 5
Printing the arguments of the command line
C:/Users/hp/PycharmProjects/pythonProject/DemoCmdArgs.py
first
second
third
100