Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python主脚本中寻找.py脚本时传递变量_Python - Fatal编程技术网

如何在python主脚本中寻找.py脚本时传递变量

如何在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

我想在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 = "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()
来维护该行为。这是否回答了您的问题?