Q. Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space.


You can understand by Watching video :-



Answer :-

file1 = open("Path Walla.txt","r")
file2 = open("Portal Express.txt","w")

lst = file1.readlines()

for i in lst :
    word = i.split()
    file2.write( " ".join(word)  )
    file2.write("\n")

print("Program has successfully run")

file2.close()
file1.close()

OR

file1 = open("portal.txt","r")
file2 = open("Express.txt","w")
lst = file1.readlines()

for i in lst :
    word = i.split()
    for j in word :
        file2.write( j + " ")
    file2.write("\n")

print("Program has successfully run")

file2.close()
file1.close()

Path Walla.txt Contains :-

This           is the Path Walla               Website.
We are going to do some Python code

Like a joy on the heart of a sorrow,
The sunset hangs on a                cloud;
A             golden storm of glittering sheaves,
Of fair and frail                    and fluttering leaves,
The wild wind blows in a cloud.

Hark to a voice that is                 calling
To                   my heart in the voice of the wind:
My heart is weary and sad and alone,
For its dreams                    like the fluttering leaves have gone,
And              why should I stay behind?

Output :-

Program has successfully run
>>> 

Portal Express.txt Contains :-

This is the Path Walla Website.
We are going to do some Python code

Like a joy on the heart of a sorrow,
The sunset hangs on a cloud;
A golden storm of glittering sheaves,
Of fair and frail and fluttering leaves,
The wild wind blows in a cloud.

Hark to a voice that is calling
To my heart in the voice of the wind:
My heart is weary and sad and alone,
For its dreams like the fluttering leaves have gone,
And why should I stay behind?

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post