Answers for "python delete text file"

36

python os remove file

import os
os.remove("filename.txt")
Posted by: Guest on February-11-2020
2

python delete text in text file

# Delete all text in a text file
f = open("text_file.txt", "r+")
f.truncate(0)
f.close()
Posted by: Guest on April-30-2021
0

Deleting a file using python

import os

# os.remove("text.txt")
# print("File deleted")


for file in os.listdir("fileDirectory"):
    os.remove("fileDirectory"+file)
    print(file, "deleted")
Posted by: Guest on July-16-2021
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 "python delete text file"

Python Answers by Framework

Browse Popular Code Answers by Language