You can use sys.argv
for that:
(您可以为此使用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)])
And in b.py you can access that number:
(在b.py中,您可以访问该号码:)
#b.py
import sys
random_number_1 = float(sys.argv[1])
print(sys.argv[1])
So what is sys.argv
?
(那么sys.argv
是什么?)
For example when you write that in command prompt: (例如,当您在命令提示符下编写该代码时:)
C:> pip install my_module
In pip program it access to "install" and "my_module" like that:
(在pip程序中,可以像这样访问“安装”和“ my_module”:)
import sys
sys.argv[0] # full path of program (for example C:UsersUserAppDataLocalProgramsPythonPython37Scriptspip.exe)
sys.argv[1] # "install"
sys.argv[2] # "my_module"
In this way you can only pass that number for one time, it will not change in b.py if you change it in a.py.
(这样,您只能将该数字传递一次,如果在a.py中进行更改,则该数字在b.py中将不会更改。)
Also if you want you can write that number to a file: (另外,如果您愿意,可以将该数字写入文件中:)
#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'])
And in b.py you can read that file:
(在b.py中,您可以读取该文件:)
#b.py
import pickle
with open("C:\data.txt","rb") as f:
random_number_1 = pickle.load(f)
print(random_number_1)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…