Created
February 26, 2019 12:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Naming file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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