Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to convert docstring in python code to raw string for the whole project at once.
Usage: Place this file at the root of your project where this code will recursively change docstring to raw string in every py file.
You can also specify the path.
`python <path>`
Note: Path should be Unix style. Haven't tested in Windows so not sure if it'll work or not.
import os
import sys
def convert(file):
print 'processing %s' % file
with open(file, 'r') as fr:
function_start = False
data = ''
for line in fr.readlines():
if function_start:
if line.find(' r"""') != -1:
function_start = False
line = line.replace(' """', ' r"""')
function_start = False
if line.find('def') > -1:
function_start = True
with open(file, 'w') as fw:
def index(path):
print path
for root, dirs, files in os.walk(path):
cwd = os.path.relpath(root)
print 'current directory : %s' % os.path.relpath(cwd)
for file in files:
if file.endswith('.py'):
convert('{}/{}'.format(cwd, file))
if __name__ == '__main__':
path = sys.argv[1]
except IndexError:
path = '.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment