I have to add from constants import MY_CONSTANT
to 100 python files.
Is there a simple and efficient way to do it?
I have to add from constants import MY_CONSTANT
to 100 python files.
Is there a simple and efficient way to do it?
You can just treat python files like any other text files and write to them. Assuming they are all in the same folder (if not just adapt the loop to go through the relevant folders):
for file in os.listdir(folderpath):
if file.endswith('.py'):
with open(file,'r+') as f:
content = f.read()
f.seek(0, 0)
f.write('from constants import MY_CONSTANT' + '\n' + content)