是操作系统()和子流程调用()有什么不同吗?

2024-04-19 08:49:34 发布

您现在位置:Python中文网/ 问答频道 /正文

以下两种方法在功能上有区别吗?你知道吗

os.system("echo $HOME")
subprocess.call("echo $HOME")

这是一个类似于this one的问题,但这个问题实际上更关注于subprocess.Popen()。你知道吗


Tags: 方法echo功能homeoscallthissystem
1条回答
网友
1楼 · 发布于 2024-04-19 08:49:34

如果您在windows上运行python(cpython)<built-in function system>os.system将在幕后执行_wsystem,而如果您使用的是非windows操作系统,它将使用system。你知道吗

而子流程调用将在windows上使用CreateProcess,在基于posix的操作系统上使用_posixsubprocess.fork_exec。你知道吗

以上几点应该回答你关于主要差异的问题(结构上)。。。也就是说,我建议您遵循os.system文档中最重要的建议,即:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

相关问题 更多 >