Exercice 54
1) Write a Python program that create a txt file called myfile.txt and write on it the texte: "Python is object oriented programming language".
2) Write an ohter python program that removes the 3rd word from this file.
Question1
# creating and opening file in write mode
f = open('myfile.txt' , 'w')
f.write("Python is object oriented programming language")
f.close()
Question2
# open file in read mode
f = open('myfile.txt' , 'r')
# getting file content
content = f.read()
f.close()
# converting content to a list
L = content.split()
# removing the 3rd element
L.pop(2)
# opening file in write mode by overwriting the existing content
f = open('myfile.txt' , 'w')
#building file content
for word in L:
f.write(word + " ")
f.close()
Younes Derfoufi
my-courses.net
my-courses.net