Detailed Python script automatically generates the required file instance code

Source: Internet
Author: User
Python script automatically generates required files

In the work we often need to write another file through a file, but since it is the corresponding relationship can certainly summarize the rules for the computer to help us to complete, today we have a generic file generated by the Python script to achieve this function, we will be free from the daily duplication of labor!

Define a function

def PRODUCEBNF (infilename,outfilename):  list=[]  with open (Infilename, ' R ') as INF: for line in    Inf.readlines ():      list.append (Re.match ("Regular expression"). Group (?))  With open (Outfilename, ' W ') as Outf:    i=0    outf.write ("File header");    For command in List:        outf.write ("Write to the content you just read (and possibly a corresponding relationship)")        Outf.write ("write Something Else")    outf.write ("Write file Footer")

Almost, in most cases, this is the framework, this function requires an input file, an output file, in general, we want to be able to import the input file parameters from the command line, and then generate output files in this directory

Get Input/output path

INFILE=SYS.ARGV[1]PRODUCEBNF (Infile,os.path.join (Os.path.dirname (infile), "name of the output file"));

OK, get it done, and then on the command line we execute the Python script and then add a parameter to complete it.

The script is used to help us simplify the repetitive work, let us do more meaningful, more need to think about things, so we hope that we will be more in the future with the script to simplify their repetitive work.

Thank you for reading, hope to help everyone, thank you for the support of this site!

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.