Skip to content

Instantly share code, notes, and snippets.

@tetafro
Created February 26, 2019 12:18
Show Gist options
  • Save tetafro/85b9da429c872d1a839289761984ccd7 to your computer and use it in GitHub Desktop.
Save tetafro/85b9da429c872d1a839289761984ccd7 to your computer and use it in GitHub Desktop.
Iterate over all files in directory and add a line between the 1st and the 2nd
#!/usr/bin/env python3
import os
newline = "hello world\n"
def fix(path):
print(path)
with open(path, 'r') as f:
lines = f.readlines()
lines = lines[:1] + [newline] + lines[1:]
with open(path, 'w') as f:
f.writelines(lines)
root = '.'
for dir, _, files in os.walk(root):
for fname in files:
path = '%s/%s' % (dir, fname)
fix(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment