Why use Python's os module methods instead of executing shell commands directly?

前端 未结 6 1076
耶瑟儿~
耶瑟儿~ 2021-01-29 17:58

I am trying to understand what is the motivation behind using Python\'s library functions for executing OS-specific tasks such as creating files/directories, changing file attri

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-29 18:30

    It's far more efficient. The "shell" is just another OS binary which contains a lot of system calls. Why incur the overhead of creating the whole shell process just for that single system call?

    The situation is even worse when you use os.system for something that's not a shell built-in. You start a shell process which in turn starts an executable which then (two processes away) makes the system call. At least subprocess would have removed the need for a shell intermediary process.

    It's not specific to Python, this. systemd is such an improvement to Linux startup times for the same reason: it makes the necessary system calls itself instead of spawning a thousand shells.

提交回复
热议问题