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

查看:16
本文介绍了为什么使用 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. fasteros.systemsubprocess.call 创建新进程,这对于这么简单的事情是不必要的.实际上,带有 shell 参数的 os.systemsubprocess.call 通常会创建至少两个新进程:第一个是 shell,第二个是您正在运行的命令(如果它不是像 test 这样的内置 shell).

  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) 无论文件名是什么都可以工作,而 os.spawn("chmod 777 " + path) 如果文件名是类似 ;rm -rf ~.(请注意,如果您使用不带 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天全站免登陆