Exercise 61
Write a program in Python allowing to delete multiple spaces in a text file named myfile.txt which contains a text: T = 'Python is programming language'
Solution
import os
# opening file in read mod
file = open("myfile.txt" , "r")
# retrieving the file content in string type
content = file.read()
file.close()
# converting the string content to a list
L = content.split()
# opening the file in write mod by overwriting its content
file = open("myfile.txt" , "w")
# browsing through list items
for word in L:
file.write(word + " ")
file.close()
os.startfile('myfile.txt')
Output:
Younes Derfoufi
my-courses.net
[…] Exercise 61 || Solution […]