Answers for "how to delete all text from a txt file in python"

7

clearing all text from a file in python

fileVariable = open('textDocName.txt', 'r+')
fileVariable.truncate(0)
fileVariable.close()
Posted by: Guest on September-10-2020
0

deleting in a text file in python

#get list of lines.
a_file = open("sample.txt", "r") 
lines = a_file. readlines()
a_file. close()
#remove line from list
del lines[1] delete lines.
#write new contents into file
new_file = open("sample.txt", "w+") write to file without line.
for line in lines:
new_file. write(line)
new_file. close()
Posted by: Guest on June-17-2021

Code answers related to "how to delete all text from a txt file in python"

Python Answers by Framework

Browse Popular Code Answers by Language