简体   繁体   English

从另一个 python 脚本调用 python 脚本

[英]Call python script from another python script

I have two scripts我有两个脚本

script_1.py脚本_1.py

import sys
import math
from datetime import datetime, timedelta
from calendar import isleap

count = sys.argv[1]
state = sys.argv[2]

f = open("myfile_c_"+count+".xml", 'a')
f.write("<state >"+state+"state "+"\n")
f.close()

it creates files (copies of a file) according to the input count variable它根据输入计数变量创建文件(文件的副本)

script_2.py脚本_2.py

import random
import subprocess
import decimal
import string
import sys
import math
from datetime import datetime, timedelta
from calendar import isleap

copy = int(sys.argv[1])
count = 0

state = random.choices( ["FeeSimple","Leasehold","Other"], weights=(80, 15, 5), k=copy)


while (count < copy):      
exec(open("script_1.py count state[int(count]").read()) // should call the first script and enter the arguments

any idea how to call the first script from the second script and enter the arguments in the while loop ?知道如何从第二个脚本调用第一个脚本并在 while 循环中输入参数吗?

在 script_2.py 的顶部放置下面的行,否则,您的 script_2.py 中有另一个名为 count 的变量,因此将其中一个更改为另一个名称以避免错误。

from script_1 import count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM