Skip to content

Instantly share code, notes, and snippets.

@JellyWX
Last active March 1, 2018 10:42
Show Gist options
  • Save JellyWX/46646a425884714e05801965614c6385 to your computer and use it in GitHub Desktop.
Save JellyWX/46646a425884714e05801965614c6385 to your computer and use it in GitHub Desktop.
Python script converts indentation sizes for python files
import sys
try:
filename = sys.argv[1]
indents = int(sys.argv[2])
except ValueError:
raise ValueError('2nd arg must be integer')
except IndexError:
raise IndexError('Missing args')
with open(filename, 'r') as f:
lines = f.readlines()
open(filename, 'w').close()
get_indent = [l for l in lines if l.startswith(' ')]
size = 0
for char in get_indent[0]:
if char == ' ':
size += 1
else:
break
print('Indentation size detected: {}'.format(size))
new_lines = []
for line in lines:
current_indentation = 0
for char in line:
if char == ' ':
current_indentation += 1
else:
break
if current_indentation:
scale = current_indentation / size
desired = int(scale * indents)
string = ' ' * desired
new_lines.append(string + line.strip() + '\n')
continue
new_lines.append(line)
with open(filename, 'a') as f:
for line in new_lines:
f.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment