- Author:
- liaobaocheng
- Posted:
- October 9, 2017
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
add comment "# coding:utf8# to all python file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #management/commands/addcomment.py
# coding:utf8
import os
from django.core.management import BaseCommand
class Command(BaseCommand):
""" usage: python manage.py addcomment"""
help = u'add comment *#coding:utf8* to all python file'
def handle(self, *args, **options):
walk_all_file()
def walk_all_file():
project_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
replace_file = []
for (dirpath, dirnames, filenames) in os.walk(project_dir):
for filename in filenames:
if filename.endswith('.py'):
full_path_file_name = os.sep.join([dirpath, filename])
with open(full_path_file_name, 'r+') as f:
if f.readline().startswith('#'):
print(u'already had: %s' % full_path_file_name)
else:
replace_file.append(str(full_path_file_name))
for file in replace_file:
with open(file,'r') as f:
with open('newfile.txt','w') as f2:
f2.write('# coding:utf8 \n')
f2.write(f.read())
os.rename('newfile.txt',file)
print(u'commenting : %s' % file)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Please login first before commenting.