【Python】iiacm_filemaker ——简易的.cpp文件创建即初始化脚本,ACMer专用

时间:2020-12-03 12:41:27

代码已全部重写,上次写的真是不忍直视……

今天刚刚接触Python,本着学以致用的原则,就写了一个按照要求自动生成.cpp文件并初始化头文件的脚本。

确定你的linux中安装了Python,将下面的代码拷贝进一个文件[filemaker],提权(chmod +x filemaker)

功能:

1、filemaker [文件名],即生成 文件名.cpp

2、filemaker -e [A-Z] 即生成从A到你输入的字母的所有字母.cpp

3、filemaker -n [1-26] 即生成从A开始的你输入个数的文件,大写字母递增

可以将该脚本所在的目录设置环境变量,以后就可以直接使用了

vim ~/.profile

在最后加上

export PATH="目录:$PATH"

保存退出

source .profile

即可

代码如下:

注:第七行的 headfile 是你要初始化的头文件所在的目录

 #!/usr/bin/python

 import sys
import getopt content = []
headfile = "/home/kevince/Documents/acm/head/acmhead.h" #the directory of the headfile #load file head.h and save it with a list
def loadcontent():
f = open(headfile)
lines = f.readlines()
for line in lines:
content.append(line) def Write(filename):
f = open(filename, "w")
for index, val in enumerate(content):
f.write(val)
f.close() def End(al):
num = ord(al) - ord('A') + 1
if num < 1 or num > 26:
print 'A-Z, please'
return
for i in range(0, num):
name = chr(ord('A') + i) + '.cpp'
Write(name)
return def Num(num):
num = int(num)
if num < 1 or num > 26:
print '1-26, please'
return
for i in range(0, num):
name = chr(ord('A') + i) + '.cpp'
Write(name)
return def Usage():
print "\n"
print "iiacm-filemaker [fliename]\n"
print "or\n"
print "iiacm-filemaker -n | -e\n"
print " -n number of files\n"
print " -e endplace of files\n"
return def Make(args):
Write(args[0]+'.cpp')
return #main function
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'e:n:', ['--end', '--num'])
except getopt.GetoptError:
Usage()
sys.exit()
loadcontent() #loadcontent
for o, a in opts:
if o in ('-e', '--help'):
End(a)
sys.exit()
if o in ('-n', '--num'):
Num(a)
sys.exit()
if len(args) == 0:
Usage()
else:
Make(args) if __name__ == '__main__':
main()