如何在python主脚本中寻找.py脚本时传递变量
我想在python脚本中运行old.py时传递“ref”变量。我怎么做如何在python主脚本中寻找.py脚本时传递变量,python,Python,我想在python脚本中运行old.py时传递“ref”变量。我怎么做 ref = true os.system('old.py -setting + ref') ???? (its not correct) 在主python脚本中运行old.py时,传递ref变量的最佳方法是什么 谢谢。使用字符串格式或连接 ref = 'true' os.system(f'old.py -setting {ref}') 不要使用操作系统;改用子流程 import subprocess ref = &q
ref = true
os.system('old.py -setting + ref') ???? (its not correct)
在主python脚本中运行old.py
时,传递ref
变量的最佳方法是什么
谢谢。使用字符串格式或连接
ref = 'true'
os.system(f'old.py -setting {ref}')
不要使用操作系统;改用
子流程
import subprocess
ref = "true"
subprocess.run(["old.py", "-setting", ref])
如果ref
确实是一个布尔值变量,则必须首先将其转换为字符串
import subprocess
ref = True
subprocess.run(["old.py", "-setting", str(ref).lower()])
下面的代码怎么样
import os
ref = 'true'
os.system('python old.py -setting ' + ref)
它应该在操作系统中运行以下命令
python old.py -setting true
您可能需要
str(ref).lower()
来维护该行为。这是否回答了您的问题?