Python 会很好地替代 Windows 命令行/批处理脚本吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/213798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Would Python make a good substitute for the Windows command-line/batch scripts?
提问by Lawrence Johnston
I've got some experience with Bash, which I don't mind, but now that I'm doing a lot of Windows development I'm needing to do basic stuff/write basic scripts using the Windows command-line language. For some reason said language really irritates me, so I was considering learning Python and using that instead.
我有一些使用Bash 的经验,我不介意,但现在我正在做很多 Windows 开发,我需要使用 Windows 命令行语言做基本的东西/编写基本的脚本。出于某种原因,说语言真的让我很恼火,所以我正在考虑学习 Python 并使用它。
Is Python suitable for such things? Moving files around, creating scripts to do things like unzipping a backup and restoring a SQL database, etc.
Python 适合做这样的事情吗?移动文件、创建脚本以执行诸如解压缩备份和恢复 SQL 数据库等操作。
采纳答案by gnud
Python is well suited for these tasks, and I would guess much easier to develop in and debug than Windows batch files.
Python 非常适合这些任务,我想在开发和调试中比 Windows 批处理文件要容易得多。
The question is, I think, how easy and painless it is to ensure that all the computers that you have to run these scripts on, have Python installed.
问题是,我认为,确保您必须在其上运行这些脚本的所有计算机都安装了 Python 是多么容易和轻松。
回答by e-satis
Summary
概括
Windows: no need to think, use Python. Unix: quick or run-it-once scripts are for Bash, serious and/or long life time scripts are for Python.
Windows: 不用想,用 Python。 Unix:快速或一次性脚本适用于 Bash,严肃和/或长寿命脚本适用于 Python。
The big talk
大话
In a Windows environment, Python is definitely the best choice since cmdis crappy and PowerShell has not really settled yet. What's more Python can run on several platform so it's a better investment. Finally, Python has a huge set of library so you will almost never hit the "god-I-can't-do-that" wall. This is not true for cmd and PowerShell.
在 Windows 环境中,Python 绝对是最佳选择,因为cmd很糟糕,而 PowerShell 还没有真正解决。更重要的是,Python 可以在多个平台上运行,因此这是一项更好的投资。最后,Python 拥有庞大的库集,因此您几乎永远不会碰到“上帝-我-我-不能-那样做”的墙。这不适用于 cmd 和 PowerShell。
In a Linux environment, this is a bit different. A lot of one liners are shorter, faster, more efficient and often more readable in pure Bash. But if you know your quick and dirty script is going to stay around for a while or will need to be improved, go for Python since it's far easier to maintain and extend and you will be able to do most of the task you can do with GNU tools with the standard library. And if you can't, you can still call the command-line from a Python script.
在 Linux 环境中,这有点不同。在纯 Bash 中,许多 one liner 更短、更快、更高效并且通常更具可读性。但是,如果您知道您的快速而肮脏的脚本将保留一段时间或需要改进,请选择 Python,因为它更易于维护和扩展,并且您将能够完成您可以完成的大部分任务带有标准库的 GNU 工具。如果不能,您仍然可以从 Python 脚本调用命令行。
And of course you can call Python from the shell using -c option:
当然,您可以使用 -c 选项从 shell 调用 Python:
python -c "for line in open('/etc/fstab') : print line"
Some more literature about Python used for system administration tasks:
关于用于系统管理任务的 Python 的更多文献:
回答by Vinko Vrsalovic
Sure, python is a pretty good choice for those tasks (I'm sure many will recommend PowerShell instead).
当然,对于这些任务,python 是一个不错的选择(我相信很多人会推荐 PowerShell)。
Here is a fine introduction from that point of view:
从这个角度来看,这是一个很好的介绍:
http://www.redhatmagazine.com/2008/02/07/python-for-bash-scripters-a-well-kept-secret/
http://www.redhatmagazine.com/2008/02/07/python-for-bash-scripters-a-well-kept-secret/
EDIT: About gnud's concern: http://www.portablepython.com/
编辑:关于 gnud 的关注:http: //www.portablepython.com/
回答by bdukes
Are you aware of PowerShell?
你知道PowerShell吗?
回答by technomalogical
@BKB definitely has a valid concern. Here's a couple links you'll want to check if you run into any issues that can't be solved with the standard library:
@BKB 肯定有一个合理的担忧。如果您遇到标准库无法解决的任何问题,您需要检查以下几个链接:
- Pywin32is a package for working with low-level win32 APIs (advanced file system modifications, COM interfaces, etc.)
- Tim Golden's Python page: he maintains a WMI wrapper package that builds off of Pywin32, but be sure to also check out his "Win32 How Do I"page for details on how to accomplish typical Windows tasks in Python.
- Pywin32是一个用于处理低级 win32 API(高级文件系统修改、COM 接口等)的包
- Tim Golden 的 Python 页面:他维护了一个基于 Pywin32 构建的 WMI 包装程序包,但请务必查看他的“Win32 How Do I”页面,了解有关如何在 Python 中完成典型 Windows 任务的详细信息。
回答by Nick
Anything is a good replacement for the Batch file system in windows. Perl, Python, Powershell are all good choices.
任何东西都可以很好地替代 Windows 中的批处理文件系统。Perl、Python、Powershell 都是不错的选择。
回答by Greg Hewgill
Python is certainly well suited to that. If you're going down that road, you might also want to investigate SConswhich is a build system itself built with Python. The cool thing is the build scripts are actually full-blown Python scripts themselves, so you can do anything in the build script that you could otherwise do in Python. It makes make
look pretty anemic in comparison.
Python当然非常适合这一点。如果您要走这条路,您可能还想研究SCons,它是一个本身用 Python 构建的构建系统。很酷的事情是构建脚本本身实际上是成熟的 Python 脚本,因此您可以在构建脚本中执行您可以在 Python 中执行的任何操作。make
相比之下,它看起来非常贫血。
Upon rereading your question, I should note that SCons is more suited to building software projects than to writing system maintenance scripts. But I wouldn't hesitate to recommend Python to you in any case.
重新阅读您的问题后,我应该注意到 SCons 更适合构建软件项目,而不是编写系统维护脚本。但无论如何我都会毫不犹豫地向您推荐 Python。
回答by Bullines
回答by Lawrence Johnston
As a follow up, after some experimentation the thing I've found Python most useful for is any situation involving text manipulation (yourStringHere.replace(), regexes for more complex stuff) or testing some basic concept really quickly, which it is excellent for.
作为跟进,经过一些实验,我发现 Python 最有用的东西是任何涉及文本操作的情况(yourStringHere.replace(),更复杂的东西的正则表达式)或非常快速地测试一些基本概念,这非常适合.
For stuff like SQL DB restore scripts I find I still usually just resort to batch files, as it's usually either something short enough that it actually takes more Python code to make the appropriate system calls or I can reuse snippets of code from other people reducing the writing time to just enough to tweak existing code to fit my needs.
对于 SQL DB 恢复脚本之类的东西,我发现我仍然通常只是求助于批处理文件,因为它通常足够短以至于实际上需要更多的 Python 代码来进行适当的系统调用,或者我可以重用其他人的代码片段以减少编写时间刚好足以调整现有代码以满足我的需要。
As an addendum I would highly recommend IPythonas a great interactive shell complete with tab completion and easy docstring access.
作为一个附录,我强烈推荐IPython作为一个出色的交互式 shell,它具有制表符补全和简单的文档字符串访问功能。
回答by Jay
I've done a decent amount of scripting in both Linux/Unix and Windows environments, in Python, Perl, batch files, Bash, etc. My advice is that if it's possible, install Cygwin and use Bash (it sounds from your description like installing a scripting language or env isn't a problem?). You'll be more comfortable with that since the transition is minimal.
我已经在 Linux/Unix 和 Windows 环境中使用 Python、Perl、批处理文件、Bash 等编写了大量脚本。我的建议是,如果可能,安装 Cygwin 并使用 Bash(从您的描述中听起来像安装脚本语言或环境不是问题?)。你会更舒服,因为过渡是最小的。
If that's not an option, then here's my take. Batch files are very kludgy and limited, but make a lot of sense for simple tasks like 'copy some files' or 'restart this service'. Python will be cleaner, easier to maintain, and much more powerful. However, the downside is that either you end up calling external applications from Python with subprocess, popen or similar. Otherwise, you end up writing a bunch more code to do things that are comparatively simple in batch files, like copying a folder full of files. A lot of this depends on what your scripts are doing. Text/string processing is going to be much cleaner in Python, for example.
如果这不是一个选项,那么这就是我的看法。批处理文件非常混乱且有限,但对于“复制某些文件”或“重新启动此服务”等简单任务很有意义。Python 将更简洁、更易于维护且功能更强大。但是,缺点是您最终会使用子进程、popen 或类似的方式从 Python 调用外部应用程序。否则,您最终会编写更多代码来执行批处理文件中相对简单的操作,例如复制一个装满文件的文件夹。这在很大程度上取决于您的脚本在做什么。例如,文本/字符串处理将在 Python 中更加清晰。
Lastly, it's probably not an attractive alternative, but you might also consider VBScript as an alternative. I don't enjoy working with it as a language personally, but if portability is any kind of concern then it wins out by virtue of being available out of the box in any copy of Windows. Because of this I've found myself writing scripts that were unwieldy as batch files in VBScript instead, since I can't usually depend on Python or Perl or Bash being available on Windows.
最后,它可能不是一个有吸引力的替代方案,但您也可以考虑将 VBScript 作为替代方案。我个人不喜欢将它作为一种语言来使用,但是如果考虑到可移植性,那么它会因在任何 Windows 副本中开箱即用而胜出。因此,我发现自己在 VBScript 中编写了笨拙的脚本作为批处理文件,因为我通常不能依赖 Python、Perl 或 Bash 在 Windows 上可用。