Home > Backend Development > Python Tutorial > Detailed explanation of Python script automatically generates required file example code

Detailed explanation of Python script automatically generates required file example code

高洛峰
Release: 2017-02-06 13:22:22
Original
1315 people have browsed it

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("写入文件尾")
Copy after login

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),"输出文件的名字"));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template