Write a python program to create a text file using file object, write 2 lines in it and then delete the same text file.
Answers
Answered by
1
Answer:
that’s pretty easy one, instead of opening file with ‘w’ (write) flag, to create/recreate new file you should open existing file using ‘a’ (append) flag to append new text to existing data, like below:
import os #optional
file_path = "/path/to/file.txt"
if os.path.exists(file_path): #optional check if file exists
with open(file_path, 'a') as file:
file.write("\n") # could be any text, appended @ the end of file
Similar questions