在 Python 中使用 os.system 调用多个命令 [英] Calling multiple commands using os.system in Python
问题描述
我想从我的 python 脚本中调用多个命令.我尝试使用 os.system(),但是,当当前目录更改时,我遇到了问题.
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.
示例:
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.
推荐答案
os.system
是标准 C 函数 system()
,因此它的参数可以是any有效的shell 命令,只要它适合为进程的环境和参数列表保留的内存.
os.system
is a wrapper for Standard C function system()
, and thus its argument can be any valid shell command as long as it fits into the memory reserved for environment and argument lists of a process.
因此,将这些命令用分号或换行符分隔,它们将在同一环境中按顺序执行.
So, separate those commands with semicolons or line breaks, and they will be executed sequentially in the same environment.
os.system(" ls -l; <some command>; launchMyApp")
os.system('''
ls -l
<some command>
launchMyApp
''')
这篇关于在 Python 中使用 os.system 调用多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!