The python script automatically generates the required file instance code

  • 2020-05-26 09:23:25
  • OfStack

The python script automatically generates the required files

In the work, we often need to write another file by one file, but since it is the corresponding relationship can definitely summarize the rules for the computer to help us complete, today we will use a common file generated by python script to achieve this function, will everyone from the daily repetitive labor liberation!

Define 1 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(" The file header ");
    for command in List:
        outf.write( "Write what you just read (or maybe some correspondence)." )
        outf.write(" Write something else ")
    outf.write(" Write the end of the file ")

Basically, most of the time, it's this framework, this function requires 1 input file, 1 output file, 1 in general, we want to be able to pass in the input file arguments from the command line, and then generate the output file in this directory

Get the I/o path


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

ok, done, and then on the command line we execute this python script and then we add a parameter and we're done.

Script is used to help us simplify the repetitive work, let us do more meaningful, more need to think about things, so I hope you will use scripts to simplify their own repetitive work in the future.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: