您可以sys.argv
为此使用:
#a.py
import random
import subprocess
def rnd():
return random.random()
random_number_1 = rnd()
if random_number_1 > 0.5:
subprocess.Popen(['C:/Users/ex/AppData/Local/Programs/Python/Python38/python.exe', 'b.py', str(random_number_1)])
在 b.py 中,您可以访问该号码:
#b.py
import sys
random_number_1 = float(sys.argv[1])
print(sys.argv[1])
那么是什么sys.argv
?例如,当您在命令提示符下编写时:
C:\> pip install my_module
在 pip 程序中,它可以像这样访问“install”和“my_module”:
import sys
sys.argv[0] # full path of program (for example C:\Users\User\AppData\Local\Programs\Python\Python37\Scripts\pip.exe)
sys.argv[1] # "install"
sys.argv[2] # "my_module"
这样你只能传递那个数字一次,如果你在a.py中改变它,它在b.py中不会改变。此外,如果您愿意,可以将该数字写入文件:
#a.py
import random
import subprocess
import pickle
def rnd():
return random.random()
random_number_1 = rnd()
with open("C:\\data.txt","wb") as f:
f.write(pickle.dumps(random_number_1))
if random_number_1 > 0.5:
subprocess.Popen(['C:/Users/ex/AppData/Local/Programs/Python/Python38/python.exe', 'b.py'])
在 b.py 中,您可以读取该文件:
#b.py
import pickle
with open("C:\\data.txt","rb") as f:
random_number_1 = pickle.load(f)
print(random_number_1)