码迷,mamicode.com
首页 > 编程语言 > 详细

my python script (1) ---------create model script file

时间:2016-05-16 07:04:23      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

# my  python script

#  to create script file

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os,subprocess

child = subprocess.Popen(‘which python‘,stdout = subprocess.PIPE,shell=True)
(pypath,error) = child.communicate()
pypath = pypath.strip(‘\n‘)

myfile = raw_input(‘please input file name (like home/test.py)‘)
(path,filename) = os.path.split(myfile)
if path:
    print ‘check dir path ...‘
    if os.path.exists(path):
        print ‘dir already exists!‘
    else:    
        print ‘dir: %s does not exist!‘ % path
        print ‘create dir: %s ...‘ % path
        try:
            os.makedirs(path)
            print ‘dir: %s is created successfully!‘ % path    
        except Exception ,e:
            print ‘exception occur while try to create dirs %s‘ % path
            print e
else:
    pass

print ‘create script file: %s ...‘ % filename
try:
    f = open(myfile,‘w‘)
    f.write(‘#!‘+ pypath + ‘\n‘)
    f.write(‘#! -*- coding:utf-8 -*-‘)
    f.write(‘\n‘)
    f.write(‘\n‘)
    f.write(‘if __name__ == ‘+ "‘__main__‘:\n")
    f.write(‘    print "hello world!"\n‘)
    f.close()
    print ‘script is created successfully!‘
except Exception,e:
    print ‘exception occur while try to write file: %s ‘ % filename
    print e

 

# victor

 

my python script (1) ---------create model script file

标签:

原文地址:http://www.cnblogs.com/ct-blog/p/5496818.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!