I have a relatively big repo (too big for me to do the task manually) that needs a simple refactoring at every file.
I want to iterate over all files in my repo and replace statements like:
from <old_location> import <something>
to
from <new_location> import <something>
I have written the following code
import os
def refactor_imports(rootdir=PATH):
for subdir, dirs, files in os.walk(rootdir):
for file in files:
process_file(os.path.join(subdir, file))
def process_file(path):
with open(path, 'a') as f_obj:
for line in f_obj:
if not line.startswith("from"):
return
else:
#TODO: rewrite the line
and I am not sure how to complete it, how can you rewrite the file?
Is there a way to do so without reading the entire file content in memory?