【问题标题】:How to pass variable name in subprocess command [duplicate]如何在子进程命令中传递变量名[重复]
【发布时间】:2022-01-19 13:31:33
【问题描述】:
import subprocess
result = subprocess.call('"C:\\Users\\user\\Desktop\\Install Exe\\download.exe" /S norestart ', shell=True)
print(result)

这是命令中的硬编码,我想在变量中传递这个路径并想要变量而不是路径

我们可以这样做吗?

【问题讨论】:

    标签: python subprocess


    【解决方案1】:

    随便用

    subprocess.run(["arg1", "arg2", "arg3"])
    

    所以说我想将一个文件从“/home/myfile.txt”移动到“/home/data”,它接受我会做的参数源文件和目标文件夹

    subprocess.run(["mv", "/home/myfile.txt", "/home/data"])
    

    这些字符串中的任何一个都可以是变量,并且这些字符串也可以是 f-string 格式

    【讨论】:

    • 导入子进程 source_url="github.com/notepad-plus-plus/notepad-plus-plus/releases/…" 目的地=f'C:\\Users\\user\\Desktop\\Install Exe\\download.exe' 下载=urlretrieve(source_url,destination) result = subprocess.call('C:\\Users\\user\\Desktop\\Install Exe\\download.exe /S norestart ', shell=True) print(result) 如何在 subprocess 命令中写入变量名硬代码路径
    • @RishiVashishtha,这个答案告诉你停止使用shell=True,而是传递一个单独的参数列表。您可以为这些参数中的任何一个使用变量。
    • @RishiVashishtha 如果这回答了你的问题,你介意接受吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2019-01-17
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2011-06-15
    相关资源
    最近更新 更多