Ex 7
Ex 7
AIM
THEORY:
Python has the io module that contains different functions for handling files. This
function returns a file object called file handle which is stored in the variable file_object.
We can use this variable to transfer data to and from the file (read and write) by calling
the functions defined in the Python's io module.
OUTPUT:-
21
2. Write a python script to print the contents of the file after removing the space at
the end of each line.
CODE:-
f = open("a.txt", 'r')
s = ""
for x in f.readlines():
t = list(x)
t[len(t)-1] = ""
s += "".join(t)
print(s)
OUTPUT:-
Economic and political issues inform how governments choose to respond to
anthropogenic climate change. To reduce global warming in years to come,
nations may need to implement policies with the potential to inhibit their
economies. Efforts to further tighten restrictions on greenhouse emissions may
include a market-based system such as a cap-and-trade program that limits a
firm's total greenhouse gas output but allows a firm to purchase additional
emissions credits.Regulations that place higher industry standards on
performance and technology provide another way to reduce emissions.Both
approaches, however, have the potential to reduce current production capacity,
foreign investment, and household purchasing power, as well as lead to higher
consumer prices. For these reasons, governments have encountered great
difficulty in agreeing on a global plan to deal with Earth's changing
climate.Wealthier countries produce significantly larger amounts of greenhouse
gases than poorer countries, thus contributing more to the process of global
warming.
3. Write a python script to print the specified number of characters from a file.
CODE:-
f = open("a.txt", 'r')
n = int(input("Number of Characters: "))
print(f.read(n))
OUTPUT:-
Economic and political issues inform how governments choose to respond.
4. Create a file that contains a python script and print the contents of the file in
such a way that the comment lines are removed.
CODE:-
f = open("a.py", 'r')
for x in f.readlines():
for i in x:
if i == "#":
break
else:
print(i, end = "")
print()
OUTPUT:-
def maximum(arr):
ans = mx = mn = arr[0]
for x in arr[1:]:
if x < 0:
mx, mn = mn, mx
mx = max(x, x*mx)
mn = min(x, x*mn)
return ans
5. Write a python program that counts the number of times a particular character
appears in the file.
CODE:-
f = open("a.txt", 'r')
word = input("Enter the Charcter to Search: ")
count = 0
for x in f.readlines():
for i in x.split():
if i == word:
count += 1
print(count)
OUTPUT:-
5
6. Write a program that reads data from a file and calculates the percentage of
vowels and consonants in the file.
CODE:-
v = ['a', 'e', 'i', 'o', 'u']
vowelc, consonantc, totalcount = 0 , 0, 0
f = open("a.txt", 'r')
for x in f.readlines():
for i in x:
if i.isalpha():
if i in v:
vowelc += 1
totalcount += 1
else:
consonantc += 1
totalcount += 1
print("Percentage of the vowels in the paragraph is : " ,
round((vowelc/totalcount)*100, 3), "%")
print("Percentage of the consonants in the paragraph is : " ,
round((consonantc/totalcount)*100, 3), "%")
OUTPUT:-
Percentage of the vowels in the paragraph is : 38.916 %
Percentage of the consonants in the paragraph is : 61.084 %
RESULT:
The above program to perform File handling in python are executed successfully