Thanks to visit codestin.com
Credit goes to www.scribd.com

100% found this document useful (1 vote)
42 views4 pages

7-b) File Handling Examples

The document discusses various Python programs and functions related to reading and manipulating text from files. It includes examples of reading a file line by line, counting lines, finding and counting words, reading specific words by length, and writing names to a file in different formats.

Uploaded by

Praveen Krishnan
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
100% found this document useful (1 vote)
42 views4 pages

7-b) File Handling Examples

The document discusses various Python programs and functions related to reading and manipulating text from files. It includes examples of reading a file line by line, counting lines, finding and counting words, reading specific words by length, and writing names to a file in different formats.

Uploaded by

Praveen Krishnan
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/ 4

file

October 18, 2023

0.0.1 Write a function to read the content from a text file ”poem.txt” line by line
and display the same on screen.

[1]: with open('poem.txt') as f:


s = f.read()
print(s)

Where the mind is without fear and the head is held high
Where knowledge is free
Where the world has not been broken up into fragments by narrow domestic walls
Where words come out from the depth of truth
Where tireless striving stretches its arms toward perfection
Where the clear stream of reason has not lost its way into the dreary desert
sand of dead habit
Where the mind is led forward by thee into ever widening thought and action
Into that heaven of freedom, my Father, let my country awake.

0.0.2 Write a program to count the number of lines from a text file ”poem.txt” which
is not starting with an alphabet ‘W’.

[9]: with open('poem.txt') as f:


count = 0
for line in f:
if line[0] != 'W':
count += 1
print(line)
print("The number of lines from a text file ”poem.txt” which is not starting␣
↪with an alphabet 'W':", count)

Into that heaven of freedom, my Father, let my country awake.

The number of lines from a text file ”poem.txt” which is not starting with an
alphabet 'W': 1

1
0.0.3 Write a program to count and display the total number of words in a text file.

[3]: with open('poem.txt') as f:


content = f.read()
words = content.split()
print(len(words))

91

0.0.4 Write a function display_words() in python to read lines from a text file
”story.txt”, and display those words, which are less than 4 characters.

[4]: def display_word(filename):


with open(filename) as f:
content = f.read()
words = content.split()
for word in words:
if(len(word)<4):
print((word))
display_word('poem.txt')

the
is
and
the
is
is
the
has
not
up
by
out
the
of
its
the
of
has
not
its
way
the
of
the
is
led
by

2
and
of
my
let
my

0.0.5 Write a program to print the first and last word from each line in a file.

[6]: def display_word(filename):


with open(filename) as f:
for line in f:
words = line.split()
print(words[0], words[len(words)-1])
display_word('poem.txt')

Where high
Where free
Where walls
Where truth
Where perfection
Where habit
Where action
Into awake.

0.0.6 Write a program that takes 5 names from the user as input and write them in
a file names names.txt
This will save all the five names in a single line
[5]: with open('names.txt', 'w') as f:
for i in range(5):
name = input("Enter a name: ")
f.write(name)

Enter a name: Alice


Enter a name: Bob
Enter a name: Charlie
Enter a name: David
Enter a name: Ellen

[6]: !cat names.txt

AliceBobCharlieDavidEllen

This will store each of the names in separate lines


[2]: with open('names.txt', 'w') as f:
for i in range(5):
name = input("Enter a name: ")

3
f.write(name+"\n")

Enter a name: Alice


Enter a name: Bob
Enter a name: Charlie
Enter a name: David
Enter a name: Ellen

[4]: !cat names.txt

Alice
Bob
Charlie
David
Ellen

[ ]:

You might also like