Would Python make a good substitute for the Windows command-line/batch scripts?

后端 未结 12 868
醉酒成梦
醉酒成梦 2021-01-30 07:21

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 Wind

相关标签:
12条回答
  • 2021-01-30 07:49

    Sure, python is a pretty good choice for those tasks (I'm sure many will recommend PowerShell instead).

    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/

    EDIT: About gnud's concern: http://www.portablepython.com/

    0 讨论(0)
  • 2021-01-30 07:49

    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.

    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.

    As an addendum I would highly recommend IPython as a great interactive shell complete with tab completion and easy docstring access.

    0 讨论(0)
  • 2021-01-30 07:51

    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.

    The big talk

    In a Windows environment, Python is definitely the best choice since cmd is 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.

    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.

    And of course you can call Python from the shell using -c option:

    python -c  "for line in open('/etc/fstab') : print line"
    

    Some more literature about Python used for system administration tasks:

    • The IBM lab point of view.

    • A nice example to compare bash and python to script report.

    • The basics.

    • The must-have book.

    0 讨论(0)
  • 2021-01-30 07:51

    @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:

    • Pywin32 is 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.
    0 讨论(0)
  • 2021-01-30 07:51

    As much as I love python, I don't think it a good choice to replace basic windows batch scripts.

    I can't see see someone having to import modules like sys, os or getopt to do basic things you can do with shell like call a program, check environment variable or an argument.

    Also, in my experience, goto is much easier to understand to most sysadmins than a function call.

    0 讨论(0)
  • 2021-01-30 07:52

    Python is certainly well suited to that. If you're going down that road, you might also want to investigate SCons which 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.

    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.

    0 讨论(0)
提交回复
热议问题