简体   繁体   English

如何通过 Windows 上的 Python 子进程一次运行多个 shell 命令?

[英]How can I run multiple shell commands at once through a Python subprocess on Windows?

Firstly, please excuse my lack of knowledge;首先,请原谅我缺乏知识; I am a complete beginner at python.我是 python 的完整初学者。

I would like to run several command line commands from within a python script, using the subprocess module.我想使用subprocess模块从 python 脚本中运行几个命令行命令。 My python script is supposed to be an application that downloads files from a url.我的 python 脚本应该是一个从 url 下载文件的应用程序。 These commands are shown below:这些命令如下所示:

set /P _inputname= my-url-here
wget %_inputname%

(wget is in the same directory as my python script) (wget 与我的 python 脚本在同一目录中)

I have tried using this method:我尝试过使用这种方法:

cmds = ['set /P _inputname=', 'my-url-here', 'wget %_inputname%']
subprocess.run('cmds', shell=True)

However, it fails to run wget .但是,它无法运行wget

I understand that my question may seem very similar to this question and this question .我知道我的问题可能与这个问题这个问题非常相似。 However, the solutions in the aforementioned posts have not worked for me.但是,上述帖子中的解决方案对我不起作用。 Is there an alternative method?有替代方法吗?

The posts you mentioned were meant for systems with bash installed (such as most linux distros and other unix like oses)您提到的帖子适用于安装了 bash 的系统(例如大多数 linux 发行版和其他 unix 像 oses)

Though I do not own a windows machine, I presume this will work for you:虽然我没有 windows 机器,但我想这对你有用:

(use the & sign to seperate commands) (使用 & 符号分隔命令)

cmds = ['set', "/P", '_inputname={}'.format('my-url-here'), '&','wget', '%_inputname%']
subprocess.run(cmds, shell=True)

note also that cmds is the variable cmds, not the string还要注意 cmds 是变量 cmds,而不是字符串

source: How do I run two commands in one line in Windows CMD?来源: 如何在 Windows CMD 中的一行中运行两个命令?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM