Python implements sample code that automatically adds script header information

  • 2020-05-10 18:24:48
  • OfStack

preface

Each person to write the script of the format will be different, some more information will indicate the script itself, some are cut to the chase, this in small groups, in fact nothing, basic people what to do you all know, but if in the team is in trouble, because along with the increase in the number, the script into exponential growth, if everyone no unification 1 style, at the end of the day can cause very big disadvantages, so when the number of team growth, there must be a set of standards, the formation of series 1 encoding rules, so even if don't see the script concrete realization, also know what is the function of the script.

The script we share today is a script that automatically adds annotation information, including script name, author, time, description, script usage, language version, remarks, etc. Let's look at the sample code


#!/usr/bin/env python
 
 
from os.path import exists
from time import strftime
import os
 
title = raw_input("Enter a title for your script: ")
 
title = title + '.py'
 
title = title.lower()
 
title = title.replace(' ', '_')
 
if exists(title):
  print "\nA script with this name already exists."
  exit(1)
 
descrpt = raw_input("Enter a description: ")
name = raw_input("Enter your name: ")
ver = raw_input("Enter the version number: ")
div = '======================================='
 
filename = open(title, 'w')
 
date = strftime("%Y%m%d")
 
filename.write('#!/usr/bin/python')
filename.write('\n#title\t\t\t:' + title)
filename.write('\n#description\t\t:' + descrpt)
filename.write('\n#author\t\t\t:' + name)
filename.write('\n#date\t\t\t:' + date)
filename.write('\n#version\t\t:' + ver)
filename.write('\n#usage\t\t\t:' + 'python ' + title)
filename.write('\n#notes\t\t\t:')
filename.write('\n#python_version\t\t:2.6.6')
filename.write('\n#' + div * 2 + '\n')
filename.write('\n')
filename.write('\n')
 
filename.close()
 
os.system("clear") 
 
os.system("vim +12 " + title)
exit()

The script does not do too much explanation, basically is to get information, and then write to a file, no nonsense, this script is simple enough, let's finally look at the results generated:


#!/usr/bin/python
#title         :test4.py
#description      :I am test script
#author         :python technology 
#date          :20160902
#version        :0.1
#usage         :python test4.py
#notes         :
#python_version     :2.6.6
#==============================================================================

conclusion


Related articles: