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

查看:103
本文介绍了如何在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 库获得.这并不是执行 all 外部命令的最佳首选.另请参阅 shutil ,了解一些独立于Linux命令的命令,但您可能可以直接在您的Python脚本中实现. os 库中还有另一批Linux命令.您可以在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.

然后-奖金! - 更快速.外壳程序中的每个单独的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编程功能.这就是所有过程状态代码检查,各种逻辑命令(如果有,为……等),测试命令及其所有亲属.函数定义的东西.在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 ) | something >result之类的东西.这将并行运行两个进程(将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"模块.不要从开始编写替换"grep"的Python模块开始.

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天全站免登陆