0% found this document useful (0 votes)
10 views5 pages

Ex 7

The document outlines a Python program aimed at demonstrating file handling operations using the io module. It includes examples of code to count lines, remove trailing spaces, read a specified number of characters, and calculate the percentage of vowels and consonants in a file. Additionally, it lists various file methods available in Python for handling files.

Uploaded by

Aristo Vince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Ex 7

The document outlines a Python program aimed at demonstrating file handling operations using the io module. It includes examples of code to count lines, remove trailing spaces, read a specified number of characters, and calculate the percentage of vowels and consonants in a file. Additionally, it lists various file methods available in Python for handling files.

Uploaded by

Aristo Vince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex.

7 File handling in python

AIM

To create a python program to perform operations of File handling in Python.

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.

1. Write a python program to print the number of lines in a file.


CODE:-
f = open("a.txt", 'r')
print(len(f.readlines()))
f.close()

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)

ans = max(ans, mx)

return ans

l = list(int(x) for x in input().split())


print(maximum(l))

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 %

7. Illustrate all file methods.

Python File Handling Methods:


open() Opens the file
close() Closes the file
read() Returns the file content
readable() Returns whether the file stream can be read or not
readline() Returns one line from the file
readlines() Returns a list of lines from the file
seek() Change the file position
seekable() Returns whether the file allows us to change the file position
tell() Returns the current file position
writable() Returns whether the file can be written to or not
write() Writes the specified string to the file
writelines()Writes a list of strings to the file

RESULT:

The above program to perform File handling in python are executed successfully

You might also like