Skip to content

Instantly share code, notes, and snippets.

@alitoufighi
Created December 11, 2018 07:24
Show Gist options
  • Save alitoufighi/55668b83d90b32ac884048dc36c65772 to your computer and use it in GitHub Desktop.
Save alitoufighi/55668b83d90b32ac884048dc36c65772 to your computer and use it in GitHub Desktop.
# open all files with .h in directory
# add #ifndef _[FILENAME_H]_\n#define _[FILENAME_H]_ in the beginning of the file
# add #endif at the end of the file
import glob, os, sys
def name_header_creator(filename):
name = filename.split('.')[0].upper()
header_name = f'_{name}_H_'
res = f'#ifndef {header_name}\n#define {header_name}'
return res
if(len(sys.argv) != 2):
print("Enter path to your directory in first argument.")
exit()
os.chdir(sys.argv[1])
for file in glob.glob("*.h"):
# print(file)
with open(file, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(name_header_creator(file).rstrip('\r\n') + '\n' + content + '\n' + '#endif'.rstrip('\r\n') + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment