<i id='CKGGw'><tr id='CKGGw'><dt id='CKGGw'><q id='CKGGw'><span id='CKGGw'><b id='CKGGw'><form id='CKGGw'><ins id='CKGGw'></ins><ul id='CKGGw'></ul><sub id='CKGGw'></sub></form><legend id='CKGGw'></legend><bdo id='CKGGw'><pre id='CKGGw'><center id='CKGGw'></center></pre></bdo></b><th id='CKGGw'></th></span></q></dt></tr></i><div id='CKGGw'><tfoot id='CKGGw'></tfoot><dl id='CKGGw'><fieldset id='CKGGw'></fieldset></dl></div>

<legend id='CKGGw'><style id='CKGGw'><dir id='CKGGw'><q id='CKGGw'></q></dir></style></legend>

    <bdo id='CKGGw'></bdo><ul id='CKGGw'></ul>
<tfoot id='CKGGw'></tfoot>

      <small id='CKGGw'></small><noframes id='CKGGw'>

    1. 子进程中带有变量的命令行

      Command line with variables in subprocess(子进程中带有变量的命令行)

          <small id='hqGdJ'></small><noframes id='hqGdJ'>

        1. <i id='hqGdJ'><tr id='hqGdJ'><dt id='hqGdJ'><q id='hqGdJ'><span id='hqGdJ'><b id='hqGdJ'><form id='hqGdJ'><ins id='hqGdJ'></ins><ul id='hqGdJ'></ul><sub id='hqGdJ'></sub></form><legend id='hqGdJ'></legend><bdo id='hqGdJ'><pre id='hqGdJ'><center id='hqGdJ'></center></pre></bdo></b><th id='hqGdJ'></th></span></q></dt></tr></i><div id='hqGdJ'><tfoot id='hqGdJ'></tfoot><dl id='hqGdJ'><fieldset id='hqGdJ'></fieldset></dl></div>
        2. <tfoot id='hqGdJ'></tfoot>

            <bdo id='hqGdJ'></bdo><ul id='hqGdJ'></ul>
              <legend id='hqGdJ'><style id='hqGdJ'><dir id='hqGdJ'><q id='hqGdJ'></q></dir></style></legend>

                <tbody id='hqGdJ'></tbody>

              1. 本文介绍了子进程中带有变量的命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想在脚本的子进程中使用变量运行此命令.

                I want to run this command with variables inside a subprocess in a script.

                变量是:

                filenames[k]
                

                filenames 有很多名称(字符串),我可以使用 k 来处理.

                filenames have many names (strings), which I can go through with k.

                命令是:

                python3 train.py "C:UsersTommydata\"+filenames[k] "C:UsersTommydata\"+filenames[k]+"_model" --choice A
                

                我想在脚本中作为子进程运行这个命令:

                I want to run this command inside a script as subprocess:

                subprocess.run([" python3 train.py "C:UsersTommydata\"+filenames[k] "C:UsersTommydata\"+filenames[k]+"_model" --choice A "])
                

                但是语法有问题.我不知道是什么.我在 Windows 上使用 Python 3.6.8 运行它.

                But something is wrong with the syntax. I don't know what. I run this with Python 3.6.8 on Windows.

                推荐答案

                我认为你迷失了字符串连接......你必须通过 run 或类似的方式执行 cmd:要么作为字符串或列表(因为列表通常更具可读性!)

                I think you get lost with the string concatenation... you have to ways to execute a cmd with runor such: either as a string or as a list (as a list is usually more readable!)

                案例:args as string

                Case: args as string

                cmd = f'python3 train.py "C:UsersTommydata\{filenames[k]}" "C:UsersTommydata\{filenames[k]}+_model" --choice A'
                
                subprocess.run(args=cmd, shell=True)
                

                案例:args as list

                Case: args as list

                cmd = f'python3 train.py "C:UsersTommydata\{filenames[k]}" "C:UsersTommydata\{filenames[k]}+_model" --choice A'
                
                cmd = cmd.split(' ') # provided that no white spaces in the paths!!
                
                subprocess.run(args=cmd, shell=False)
                

                备注:

                • 新"非常好用.字符串连接 f"smt {variable} smt else" 其中 variable 是之前定义的变量

                • it is very handy the "new" string concatenation f"smt {variable} smt else" where variable is a variable defined before

                如果你希望你的程序从 shell 启动,那么你需要添加一个 kwrags 参数 shell=True,默认 False.在这种情况下,根据您选择 args 是字符串还是列表,您应该更加小心:从文档中如果 shell 为 True,建议传递 `args作为一个字符串而不是一个序列"

                if you want that your program will be launched from the shell then you need to add a kwrags parameter shell=True, default False. In this case, depending if you choose the args to be either a string or a list, you should be then more careful: from the docs "if shell is True, it is recommended to pass `args as a string rather than as a sequence"

                查看 docs,位于 Popen Constructor,获取签名的完整描述

                Have a look to the docs, at Popen Constructor, for a full description of the signature

                这篇关于子进程中带有变量的命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                <tfoot id='1o18t'></tfoot>
                  <bdo id='1o18t'></bdo><ul id='1o18t'></ul>

                        <tbody id='1o18t'></tbody>
                      <legend id='1o18t'><style id='1o18t'><dir id='1o18t'><q id='1o18t'></q></dir></style></legend>
                    1. <small id='1o18t'></small><noframes id='1o18t'>

                        <i id='1o18t'><tr id='1o18t'><dt id='1o18t'><q id='1o18t'><span id='1o18t'><b id='1o18t'><form id='1o18t'><ins id='1o18t'></ins><ul id='1o18t'></ul><sub id='1o18t'></sub></form><legend id='1o18t'></legend><bdo id='1o18t'><pre id='1o18t'><center id='1o18t'></center></pre></bdo></b><th id='1o18t'></th></span></q></dt></tr></i><div id='1o18t'><tfoot id='1o18t'></tfoot><dl id='1o18t'><fieldset id='1o18t'></fieldset></dl></div>