一个python文件是否可以写入另一个python文件?

时间:2019-12-30 15:01:55

标签: python code-generation

我正在为python创建工作表制作工具。我可以让python创建另一个.py文件并写入该文件吗?

class ws:

    def _init_(self, number, ctype):
        self.number=wsno
        self.ctype=plang
        x=input('Questions file:')
        y=open(x, mode='r')
        z=f.read()
        a,b,c,d,e=z.split(';')
        wsno=input('Worksheet number: ')
        wspl=input('Language: ')
        title=wspl+" Worksheet "+wsno
        q1="1: "+ws.a
        q2="2: "+ws.b
        q3="3: "+ws.c
        q4="4: "+ws.d
        q5="5: "+ws.e

def renderws():

renderws()是我要使python文件成为另一个python文件的地方。提前致谢!

1 个答案:

答案 0 :(得分:3)

这绝对有效:

>>> import os
>>> with open( 'test.py', 'w' ) as fout :
...     fout.write( 'print "hello, python"' )
... 
>>> os.system( 'python test.py' )
hello, python
0
>>> 
相关问题