2

I want to know how to delete/clear all text inside another python file, I looked through stack overflow and could not find a answer, all help appreciated. Thanks!

Pandellic
  • 25
  • 5

3 Answers3

4

Just as you would a text file:

with open("file_to_clear.py", "w") as file:
    file.write("")

I think that's what you're looking for. You were probably being misled because all the examples are on text files, but it works with Python files too!

Jack Parkinson
  • 681
  • 11
  • 35
1

Try: open('yourfile.txt', 'w').close()

lordf
  • 70
  • 1
  • 12
0

Things to look into(google terms) to get your answer:

  1. How to open/read a .py file using a python script
  2. How to overwrite/clear content of a file using python
  3. Python overwrite file
  4. Python read file. Python write file. Python delete file.

Once you have tried all of the following, and are still struggling with the task. Please review https://stackoverflow.com/help/how-to-ask, and feel to free to rework/edit your question.

Akib Rhast
  • 651
  • 4
  • 15