如何从另一个 py 文件运行 py 文件?

时间:2021-04-15 03:26:46

标签: python

标题可能不正确。让我们把它放在一个好的方式。

我们有一个主要的 py 文件。这个文件允许我们创建另一个 py 文件。并运行生成的文件本身。我们的主 py 文件保存了它创建的文件中的值。

一个简单的例子;

#MAIN.py

add_number_list = []

len_add_number_list = int( input( " How many numbers will be in the list? " )  )

for i in range(len_add_number_list):
    number = input( " Enter Number = " )
    add_number_list.append(number)          # We added the number to our list.


file1 = open( "file1.py" , "w" )    # We created a file to run later

code = ""   # We create the contens of the file1.py file

file1_variable = [ "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "j" , "k" , "l" ]

code += "def add_list("     # We create function in file1.py

count_variable=0

for i in range(len_add_number_list):
    code += file1_variable[ count_variable ]
    count_variable += 1
    if count_variable == len_add_number_list:
        code += " ): \n\t"
        break
    code += ", "


code += "\treturn "

count_variable=0

for i in range(len_add_number_list):
    code += file1_variable[count_variable]
    count_variable += 1
    if count_variable == len_add_number_list:
        break
    code += " + "

code += "\nsend = add_list("

count_variable=0

for i in range(len_add_number_list):
    code += add_number_list[ count_variable ]
    count_variable += 1
    if count_variable == len_add_number_list:
        code += ")"
        break
    code += ", "
file1.write(code)
file1.close()

文件 1.py

#FILE1.py
def add_list(a, b, c, d ): 
        return a + b + c + d

send=add_list(5,10,11,12)

上面的代码是一个例子,你可以很好地理解我想说的话。

现在我如何在控制台中运行 file1.py 文件后访问 main.py 文件的输出值?

0 个答案:

没有答案
相关问题