为什么要使用Python的os模块方法而不是直接执行shell命令? [英] Why use Python's os module methods instead of executing shell commands directly?

查看:275
本文介绍了为什么要使用Python的os模块方法而不是直接执行shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解使用Python的库函数执行特定于操作系统的任务(例如创建文件/目录,更改文件属性等)的动机是什么,而不是仅通过os.system()subprocess.call()执行这些命令?

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 attributes, etc. instead of just executing those commands via os.system() or subprocess.call()?

例如,为什么我要使用os.chmod而不是os.system("chmod...")?

For example, why would I want to use os.chmod instead of doing os.system("chmod...")?

我知道,尽可能多地使用Python的可用库方法,而不是直接执行shell命令,更像是"Pythonic".但是,从功能的角度来看,这样做还有其他动机吗?

I understand that it is more "pythonic" to use Python's available library methods as much as possible instead of just executing shell commands directly. But, is there any other motivation behind doing this from a functionality point of view?

我在这里只谈论执行简单的单行shell命令.当我们需要对任务的执行进行更多控制时,我知道例如使用subprocess模块更有意义.

I am only talking about executing simple one-line shell commands here. When we need more control over the execution of the task, I understand that using subprocess module makes more sense, for example.

推荐答案

  1. 更快os.systemsubprocess.call创建新的过程,对于这种简单的操作来说是不必要的.实际上,带有shell参数的os.systemsubprocess.call通常会创建至少两个新进程:第一个是shell,第二个是您正在运行的命令(如果不是内置的shell) -in test).

  1. It's faster, os.system and subprocess.call create new processes which is unnecessary for something this simple. In fact, os.system and subprocess.call with the shell argument usually create at least two new processes: the first one being the shell, and the second one being the command that you're running (if it's not a shell built-in like test).

某些命令在单独的过程中没用.例如,如果您运行os.spawn("cd dir/"),它将更改子进程的当前工作目录,但不会更改Python进程的当前工作目录.您需要为此使用os.chdir.

Some commands are useless in a separate process. For example, if you run os.spawn("cd dir/"), it will change the current working directory of the child process, but not of the Python process. You need to use os.chdir for that.

您不必担心shell会解释特殊的字符.不管文件名是什么,os.chmod(path, mode)都可以工作,而如果文件名是; rm -rf ~之类的内容,则os.spawn("chmod 777 " + path)将会严重失败. (请注意,如果在不使用shell参数的情况下使用subprocess.call,则可以解决此问题.)

You don't have to worry about special characters interpreted by the shell. os.chmod(path, mode) will work no matter what the filename is, whereas os.spawn("chmod 777 " + path) will fail horribly if the filename is something like ; rm -rf ~. (Note that you can work around this if you use subprocess.call without the shell argument.)

您不必担心以破折号开头的文件名. os.chmod("--quiet", mode)将更改名为--quiet的文件的权限,但是os.spawn("chmod 777 --quiet")将失败,因为--quiet被解释为参数.即使对于subprocess.call(["chmod", "777", "--quiet"])也是如此.

You don't have to worry about filenames that begin with a dash. os.chmod("--quiet", mode) will change the permissions of the file named --quiet, but os.spawn("chmod 777 --quiet") will fail, as --quiet is interpreted as an argument. This is true even for subprocess.call(["chmod", "777", "--quiet"]).

您不必担心跨平台和跨shell问题,因为Python的标准库应该可以为您处理这些问题.您的系统是否具有chmod命令?安装好了吗?它支持您期望它支持的参数吗? os模块将尝试尽可能地跨平台,并在不可能的情况下进行记录.

You have fewer cross-platform and cross-shell concerns, as Python's standard library is supposed to deal with that for you. Does your system have chmod command? Is it installed? Does it support the parameters that you expect it to support? The os module will try to be as cross-platform as possible and documents when that it's not possible.

如果您正在运行的命令具有您关心的输出,则您需要对其进行解析,这比听起来要棘手,因为您可能会忘记一些特殊情况(文件名)带有空格,制表符和换行符),即使您根本不在乎可移植性.

If the command you're running has output that you care about, you need to parse it, which is trickier than it sounds, as you may forget about corner-cases (filenames with spaces, tabs and newlines in them), even when you don't care about portability.

这篇关于为什么要使用Python的os模块方法而不是直接执行shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆