Python script automatically generates required files
At work, we often need to write one file to another file. However, since it is a corresponding relationship, we can definitely summarize the rules and let the computer help us complete it. Today we will use a A python script generated from a universal file is used to implement this function, liberating everyone from daily repetitive labor!
Define a function
def produceBnf(infilename,outfilename): List=[] with open(infilename,'r') as inf: for line in inf.readlines(): List.append(re.match("正则表达式").group(?)) with open(outfilename,'w') as outf: i=0 outf.write("文件头"); for command in List: outf.write(“写入刚刚读取的内容(也可能是某种对应关系)”) outf.write("写入其他内容") outf.write("写入文件尾")
It’s almost the same. In most cases, this is the framework. This function requires an input file and an output file. Generally, We hope that we can pass in the input file parameters from the command line and then generate the output file in this directory
Get the input and output path
infile=sys.argv[1] produceBnf(infile,os.path.join(os.path.dirname(infile),"输出文件的名字"));
ok, done. Then we execute this python script on the command line and add parameters and we are done.
Scripts are used to help us simplify repetitive work and allow us to do things that are more meaningful and require more thinking. Therefore, I hope you will use scripts more in the future to simplify your repetitive work.
Thank you for reading, I hope it can help you, thank you for your support of this site!
For more detailed explanations of python scripts to automatically generate required file example codes and related articles, please pay attention to the PHP Chinese website!