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

后端 未结 4 1023
独厮守ぢ
独厮守ぢ 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:11

    Try using \n and \t

    import os
    filepath = os.getcwd()
    def MakeFile(file_name):
        temp_path = filepath + file_name
        file = open(file_name, 'w')
        file.write('def print_success():\n')
        file.write('\tprint "sucesss"')
        file.close()
        print 'Execution completed.'
    

    to output

    def print_success(): 
        print "sucesss"
    

    or multiline

    import os
    filepath = os.getcwd()
    def MakeFile(file_name):
        temp_path = filepath + file_name
        file = open(file_name, 'w')
        file.write('''
    def print_success():
        print "sucesss"
        ''')
        file.close()
        print 'Execution completed.'
    

提交回复
热议问题