在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函数 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屋!