The Quantum Garden

Home

❯

Notes

❯

Delete single line from file using Python

Delete single line from file using Python

4 June 2024

How to delete a line in text file using Python | Website Scripts And Cloud Tutorials (mistonline.in)

#!/usr/bin/python
def deleteLine():
 fn = 'myfile'
 f = open(fn)
 output = []
 str="The"
 for line in f:
   if not line.startswith(str):
    output.append(line)
 f.close()
 f = open(fn, 'w')
 f.writelines(output)
 f.close()
deleteLine()

Recent blogs

  • A short pause in blogging11 July 2026†
  • Migrating from Quartz 4 to Quartz 59 July 2026†
  • Now, 5 July 20265 July 2026
  • Now, 28 June 202628 June 2026
  • Unsustainable shame23 June 2026
See 573 more →
† means the note has changed since creation. Hover over the date to see when it was first written.

Graph View


  • Subscribe
  • About
  • |
  • Privacy
  • AI Policy
  • |
  • Mastodon
  • Github
© David C. Buchan 2002–2026 · Last updated 12 July 2026 · 1443 notes have been planted (browse the full garden) · Colophon. · total unique visits.