Replace String in All Python Files in Terminal on Mac - DevPops-Inc/python GitHub Wiki

• Type cd < directory > and press “return” key to change directory to where your Python files are located.

• Type ls and press “return” key to list the contents of the directory.
image

• Type open < Python file >.py and press “return” key to open a Python file in the default code editor.
image

• Find the string that you wish to replace.
image

• Type find . -name ‘*.py’ -print0 | xargs -0 sed -I “” ‘s/< old string >/< new string >/g’ (I used double quotes in the example because my strings contained single quotes) and press “return” key to replace a string in all Python files in the directory.
image

• You’ll see that the string has been replaced in your code editor.
image