Detailed description: python script automatically generates the required file instance code

Source: Internet
Author: User
This article describes how to use python scripts to automatically generate files.

In our work, we often need to write another file through one file. However, since it is a corresponding relationship, we can certainly sum up the rule so that the computer can help us complete it, today, we can use a python script generated by a common file to free everyone from repetitive daily work!

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 the content just read (or it may be a corresponding relationship)") outf. write ("write other content") outf. write ("end of the written file ")

In most cases, this framework is used. this function requires an input file and an output file. In general, we hope that the input file parameters can be passed in from the command line, then generate the output file in the local directory.

Obtain the input/output path

Infile = sys. argv [1] produceBnf (infile, OS. path. join (OS. path. dirname (infile), "name of the output file "));

OK. in the command line, execute the python script and add a parameter.

Scripts are used to help us simplify repetitive work, so that we can do more meaningful and more things that need to be considered. Therefore, we hope you can use your scripts to simplify your repetitive work in the future.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more details about the python script to automatically generate the required file instance code, please follow the PHP Chinese network!

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.