如何在 Python 中实现常见的 bash 习语? [英] How to implement common bash idioms in Python?

查看:25
本文介绍了如何在 Python 中实现常见的 bash 习语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前通过一堆记性不好的 AWK、sed、Bash 和一点点 Perl 来处理我的文本文件.

I currently do my textfile manipulation through a bunch of badly remembered AWK, sed, Bash and a tiny bit of Perl.

我看到有几个地方提到 python 对这种事情有好处.如何用Python替代shell脚本、AWK、sed等?

I've seen mentioned a few places that python is good for this kind of thing. How can I use Python to replace shell scripting, AWK, sed and friends?

推荐答案

任何 shell 都有几组功能.

Any shell has several sets of features.

  • 基本的 Linux/Unix 命令.所有这些都可以通过 subprocess 库获得.这并不总是执行所有外部命令的最佳首选.另请查看 shutil 以了解一些单独的 Linux 命令,但您可能可以直接在你的 Python 脚本中实现.另一大批量 Linux 命令位于 os 库中;您可以在 Python 中更简单地完成这些操作.

  • The Essential Linux/Unix commands. All of these are available through the subprocess library. This isn't always the best first choice for doing all external commands. Look also at shutil for some commands that are separate Linux commands, but you could probably implement directly in your Python scripts. Another huge batch of Linux commands are in the os library; you can do these more simply in Python.

还有——奖金!- 更快速.shell 中的每个单独的 Linux 命令(除了少数例外)派生一个子进程.通过使用 Python shutilos 模块,您无需分叉子进程.

And -- bonus! -- more quickly. Each separate Linux command in the shell (with a few exceptions) forks a subprocess. By using Python shutil and os modules, you don't fork a subprocess.

shell 环境功能.这包括设置命令环境的东西(当前目录和环境变量等等).您可以直接从 Python 中轻松管理它.

The shell environment features. This includes stuff that sets a command's environment (current directory and environment variables and what-not). You can easily manage this from Python directly.

shell 编程功能.这是所有进程状态代码检查、各种逻辑命令(if、while、for 等)、测试命令及其所有相关命令.函数定义的东西.这在 Python 中要容易得多.这是摆脱 bash 并使用 Python 实现的巨大胜利之一.

The shell programming features. This is all the process status code checking, the various logic commands (if, while, for, etc.) the test command and all of it's relatives. The function definition stuff. This is all much, much easier in Python. This is one of the huge victories in getting rid of bash and doing it in Python.

交互功能.这包括命令历史和其他内容.编写 shell 脚本不需要这个.这仅用于人机交互,不适用于脚本编写.

Interaction features. This includes command history and what-not. You don't need this for writing shell scripts. This is only for human interaction, and not for script-writing.

shell 文件管理功能.这包括重定向和管道.这更棘手.其中大部分可以通过子流程完成.但是一些在 shell 中很容易的事情在 Python 中却令人不快.特别像 (a | b; c ) | 这样的东西某事>结果.这会并行运行两个进程(a 的输出作为 b 的输入),然后是第三个进程.该序列的输出与 something 并行运行,并将输出收集到名为 result 的文件中.用任何其他语言表达这很复杂.

The shell file management features. This includes redirection and pipelines. This is trickier. Much of this can be done with subprocess. But some things that are easy in the shell are unpleasant in Python. Specifically stuff like (a | b; c ) | something >result. This runs two processes in parallel (with output of a as input to b), followed by a third process. The output from that sequence is run in parallel with something and the output is collected into a file named result. That's just complex to express in any other language.

特定程序(awk、sed、grep 等)通常可以重写为 Python 模块.不要过分.替换您需要的并改进您的grep";模块.不要一开始就编写一个 Python 模块来代替grep".

Specific programs (awk, sed, grep, etc.) can often be rewritten as Python modules. Don't go overboard. Replace what you need and evolve your "grep" module. Don't start out writing a Python module that replaces "grep".

最好的是您可以分步完成.

The best thing is that you can do this in steps.

  1. 用 Python 替换 AWK 和 PERL.其他一切都不要管.
  2. 看看用 Python 替换 GREP.这可能有点复杂,但您的 GREP 版本可以根据您的处理需求进行定制.
  3. 看看用使用 os.walk 的 Python 循环替换 FIND.这是一个巨大的胜利,因为您没有生成那么多进程.
  4. 考虑用 Python 脚本替换常见的 shell 逻辑(循环、决策等).
  1. Replace AWK and PERL with Python. Leave everything else alone.
  2. Look at replacing GREP with Python. This can be a bit more complex, but your version of GREP can be tailored to your processing needs.
  3. Look at replacing FIND with Python loops that use os.walk. This is a big win because you don't spawn as many processes.
  4. Look at replacing common shell logic (loops, decisions, etc.) with Python scripts.

这篇关于如何在 Python 中实现常见的 bash 习语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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