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!