Whats the best way to write python code into a python file?

后端 未结 4 1026
独厮守ぢ
独厮守ぢ 2021-02-07 10:47

I want to write a script (generate_script.py) generating another python script (filegenerated.py)

So far i have created generate_script.py:

import os
fil         


        
4条回答
  •  悲&欢浪女
    2021-02-07 11:15

    untubu answer is probably the more pythonic answer, but in your code example you're missing new line chars and tabs.

    file.write("def print_success():\n")
    file.write('\tprint "success"\n\n')
    

    This will give you the spacing and newlines. The link below will give you some tips on the accepted ones.

    http://docs.python.org/release/2.5.2/ref/strings.html

提交回复
热议问题