Skip to content

Instantly share code, notes, and snippets.

@basnijholt
Created June 17, 2016 13:47
Show Gist options
  • Save basnijholt/90c409b61b952748a601cd7ae16f04ff to your computer and use it in GitHub Desktop.
Save basnijholt/90c409b61b952748a601cd7ae16f04ff to your computer and use it in GitHub Desktop.
Replace or delete a line in files
# Replace a line
import os
files = [f for f in os.listdir('.') if f.endswith('md')]
for file in files:
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
if 'author: John Baltis' in line:
f.write('author: Bas Nijholt\n')
else:
f.write(line)
# Delete a line
import os
files = [f for f in os.listdir('.') if f.endswith('md')]
for file in files:
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
if 'id: ' not in line:
f.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment