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?



26 Comments

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

  1. when i run tthe program it says FileNotFoundError: [Errno 2] No such file or directory: 'portal.txt'

    ReplyDelete
    Replies
    1. please make 'portal.txt' file in your computer then run the program...

      Delete
    2. You may had not created it as u are opening in read so you must had this file in your pc

      Delete
    3. this is a common and mostly ocurring doubt,, u have to give full path of the file in header..go to the location where u have saved your file and right click on it and then click on properties , then copy the file path and paste it into the program file header.

      Delete
  2. If ..it..is.. English...it . Would be..better

    ReplyDelete
  3. this answer is wrong because the same answer is for Q1

    ReplyDelete
    Replies
    1. This is correct answer, Question no. 13 and 1 are same.

      Delete
  4. The target file is empty for me. I tried various times though there was no error.

    ReplyDelete
    Replies
    1. Please write something in portal.txt file.

      Delete
  5. please correct your spelling of help

    ReplyDelete
  6. It works well. Thank you.

    ReplyDelete
  7. Can you plz give me the Python of Sumita Arora book pdf.

    ReplyDelete
  8. bro plz give correct question it is differenr from 1st one.

    ReplyDelete
  9. Isn't this the answer of Q1?

    ReplyDelete
  10. Hey, so how we can make the protal.txt file , by writing anything????

    ReplyDelete
  11. Bro the question of 13 is different .

    Q13) write a program that will create an object called filout for writing , associate it with the filename "STRS.TXT" .The code should keep on writing strings to it as long the user wants.

    ReplyDelete

Post a Comment

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

Previous Post Next Post