Programming Examples
Python program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by single space
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 single space.
Solution
fname1=input("Enter the File Name 1 : ")
fname2=input("Enter the File Name 2 : ")
fp1 = open(fname1,"r")
fp2 = open(fname2,"w")
lines = fp1.readlines()
for line in lines :
word = line.split()
fp2.write( " ".join(word) )
fp2.write("\n")
fp1.close()
fp2.close()
Output/ Explanation:
Enter the File Name 1 : file1.txt
Enter the File Name 2 : file2.txt