首页 > 解决方案 > python文件是否可以编写另一个python文件?

问题描述

我正在为 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 文件的地方。提前致谢!

标签: pythoncode-generation

解决方案


这绝对有效:

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

推荐阅读