Python 调用多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20042205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python call multiple commands
提问by user3003701
I would like to invoke multiple commands from my python script. I tried using the os.system(), however, I'm running into issues when the current directory is changed.
我想从我的 python 脚本中调用多个命令。我尝试使用 os.system(),但是,在更改当前目录时遇到了问题。
example:
例子:
os.system("ls -l")
os.system("<some command>") # This will change the present working directory
os.system("launchMyApp") # Some application invocation I need to do.
Now, the third call to launch doesn't work.
现在,第三个启动调用不起作用。
回答by Holy Mackerel
You can change back to the directory you need to be in with os.chdir()
您可以使用 os.chdir() 改回您需要进入的目录
回答by volcano
When you call os.system(), every time you create a subshell - that closes immediately when os.systemreturns (subprocessis the recommended library to invoke OS commands). If you need to invoke a set of commands - invoke them in one call. BTW, you may change working director from Python - os.chdir
当你调用使用os.system() ,每次创建一个子shell -即关闭时立即使用os.system回报(子是推荐的库调用OS命令)。如果您需要调用一组命令 - 在一次调用中调用它们。顺便说一句,您可以从 Python 更改工作主管 - os.chdir
回答by Puffin GDI
Try to use subprocess.Popenand cwd
尝试使用subprocess.Popen和cwd
example:
例子:
subprocess.Popen('launchMyApp', cwd=r'/working_directory/')
回答by jfs
Each process has its own current working directory. Normally, child processes can't change parent's directory that is why cdis a builtin shell command: it runs in the same (shell) process.
每个进程都有自己的当前工作目录。通常,子进程无法更改父目录,这cd就是内置 shell 命令的原因:它运行在同一个(shell)进程中。
Each os.system()call creates a new shell process. Changing the directory inside these processes has no effect on the parent python process and therefore on the subsequent shell processes.
每次os.system()调用都会创建一个新的 shell 进程。更改这些进程内的目录对父 python 进程没有影响,因此对后续的 shell 进程没有影响。
To run multiple commands in the same shell instance, you could use subprocessmodule:
要在同一个 shell 实例中运行多个命令,您可以使用subprocess模块:
#!/usr/bin/env python
from subprocess import check_call
check_call(r"""set -e
ls -l
<some command> # This will change the present working directory
launchMyApp""", shell=True)
If you know the destination directory; use cwdparameter suggested by @Puffin GDI instead.
如果您知道目标目录;改用cwd@Puffin GDI 建议的参数。
回答by Cybersupernova
Just use
只需使用
os.system("first command\nsecond command\nthird command")
os.system("first command\nsecond command\nthird command")
I think you have got the idea what to do
我想你已经知道该怎么做了
回答by Anynomous
You separate the lines with &
你用 & 分隔线
os.system("ls -l & <some command> & launchMyApp")
回答by Anonymous
It's simple, really.
For Windows separate your commands with &, for Linux, separate them with ;.str.replaceis a very good way to approach the problem, used in the example below:
很简单,真的。对于 Windows,用 分隔你的命令&,对于 Linux,用;. str.replace是解决问题的一种很好的方法,在下面的示例中使用:
import os
os.system('''cd /
mkdir somedir'''.replace('\n', ';') # or use & for Windows
回答by Muthu Kumar
Try this
尝试这个
import os
os.system("ls -l")
os.chdir('path') # This will change the present working directory
os.system("launchMyApp") # Some application invocation I need to do.

