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

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

 

今天刚刚接触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 是你要初始化的头文件所在的目录

 

 1 #!/usr/bin/python

 2 

 3 import sys

 4 import getopt

 5 

 6 content = []

 7 headfile = "/home/kevince/Documents/acm/head/acmhead.h" #the directory of the headfile

 8 

 9 #load file head.h and save it with a list

10 def loadcontent():

11     f = open(headfile)

12     lines = f.readlines()

13     for line in lines:

14         content.append(line)

15 

16 def Write(filename):

17     f = open(filename, "w")

18     for index, val in enumerate(content):

19         f.write(val)

20     f.close()

21 

22 def End(al):

23     num = ord(al) - ord('A') + 1

24     if num < 1 or num > 26:

25         print 'A-Z, please'

26         return

27     for i in range(0, num):

28         name = chr(ord('A') + i) + '.cpp'

29         Write(name)

30     return

31 

32 def Num(num):

33     num = int(num)

34     if num < 1 or num > 26:

35         print '1-26, please'

36         return

37     for i in range(0, num):

38         name = chr(ord('A') + i) + '.cpp'

39         Write(name)

40     return

41         

42 

43 def Usage():

44     print "\n"

45     print "iiacm-filemaker [fliename]\n"

46     print "or\n"

47     print "iiacm-filemaker -n | -e\n"

48     print "   -n  number of files\n"

49     print "   -e  endplace of files\n"

50     return

51 

52 def Make(args):

53     Write(args[0]+'.cpp')

54     return

55 

56 #main function

57 def main():

58     try:

59         opts, args = getopt.getopt(sys.argv[1:], 'e:n:', ['--end', '--num'])

60     except getopt.GetoptError:

61         Usage()

62         sys.exit()    

63     loadcontent()  #loadcontent

64     for o, a in opts:

65         if o in ('-e', '--help'):

66             End(a)

67             sys.exit()

68         if o in ('-n', '--num'):

69             Num(a)

70             sys.exit()

71     if len(args) == 0:

72           Usage()

73     else:

74         Make(args)

75     

76 

77 

78 if __name__ == '__main__':

79     main()

 

 

你可能感兴趣的:(python)