Skip to content

Instantly share code, notes, and snippets.

Embed
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.
`python doctstring_to_raw.py`
You can also specify the path.
`python doctstring_to_raw.py <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
else:
line = line.replace(' """', ' r"""')
function_start = False
if line.find('def') > -1:
function_start = True
data+=line
with open(file, 'w') as fw:
fw.write(data)
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__':
try:
path = sys.argv[1]
except IndexError:
path = '.'
index(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment