My Python script (1)---------Create model script file

Source: Internet
Author: User
Tags python script

# my Python script

# to create script file

#!/usr/bin/env python#-*-coding:utf-8-*-ImportOs,subprocesschild= subprocess. Popen ('which Python', stdout = subprocess. Pipe,shell=True) (Pypath,error)=child.communicate () Pypath= Pypath.strip ('\ n') MyFile= Raw_input ('Please input the file name (like home/test.py)') (Path,filename)=os.path.split (myfile)ifPath:Print 'Check dir path ...'    ifos.path.exists (path):Print 'dir already exists!'    Else:            Print 'dir:%s does not exist!'%PathPrint 'Create dir:%s ...'%PathTry: Os.makedirs (path)Print 'dir:%s is created successfully!'%PathexceptException, E:Print 'exception occur while try to create dirs%s'%PathPrinteElse:    PassPrint 'Create script file:%s ...'%filenameTry: 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!'exceptexception,e:Print 'exception occur while try to write file:%s'%filenamePrintE

# Victor

My Python script (1)---------Create model script file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.