# 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
时间: 2024-12-23 14:11:24