Skip to content

Instantly share code, notes, and snippets.

@schmohlio
Last active August 10, 2017 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmohlio/536da1f4e62f8c1c582947bff1405493 to your computer and use it in GitHub Desktop.
Save schmohlio/536da1f4e62f8c1c582947bff1405493 to your computer and use it in GitHub Desktop.
Prepend text to files matching pattern in current directory
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from os.path import join
TEXT = """syntax = "proto2"\n"""
def main():
for dirpath, _, files in os.walk("."):
for f in files:
if f.endswith(".proto"):
with file(join(dirpath, f), 'r') as original: data = original.read()
with file(join(dirpath, f), 'w') as modified: modified.write(TEXT + data)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment