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

One thought on “Solution Exercise 61 delete the multiple space with python in a text file”

Leave a Reply