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

Accepted answer
Score: 25

Python is well suited for these tasks, and 6 I would guess much easier to develop in 5 and debug than Windows batch files.

The question 4 is, I think, how easy and painless it is 3 to ensure that all the computers that you 2 have to run these scripts on, have Python 1 installed.

Score: 14


Windows: no need to think, use Python. Unix: quick 22 or run-it-once scripts are for Bash, serious 21 and/or long life time scripts are for Python.

The big talk

In 20 a Windows environment, Python is definitely 19 the best choice since cmd is crappy and PowerShell 18 has not really settled yet. What's more 17 Python can run on several platform so it's 16 a better investment. Finally, Python has 15 a huge set of library so you will almost 14 never hit the "god-I-can't-do-that" wall. This 13 is not true for cmd and PowerShell.

In a 12 Linux environment, this is a bit different. A 11 lot of one liners are shorter, faster, more 10 efficient and often more readable in pure 9 Bash. But if you know your quick and dirty 8 script is going to stay around for a while 7 or will need to be improved, go for Python 6 since it's far easier to maintain and extend 5 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 4 the command-line from a Python script.

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

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

Some more literature about 1 Python used for system administration tasks:

Score: 9

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

Here is a fine introduction 2 from that point of view:


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

Score: 5

Are you aware of PowerShell?


Score: 5

Anything is a good replacement for the Batch 2 file system in windows. Perl, Python, Powershell 1 are all good choices.

Score: 5

@BKB definitely has a valid concern. Here's 3 a couple links you'll want to check if you 2 run into any issues that can't be solved 1 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.
Score: 3

Python is certainly well suited to that. If 13 you're going down that road, you might also 12 want to investigate SCons which is a build system 11 itself built with Python. The cool thing 10 is the build scripts are actually full-blown 9 Python scripts themselves, so you can do 8 anything in the build script that you could 7 otherwise do in Python. It makes make look pretty 6 anemic in comparison.

Upon rereading your 5 question, I should note that SCons is more 4 suited to building software projects than 3 to writing system maintenance scripts. But 2 I wouldn't hesitate to recommend Python 1 to you in any case.

Score: 2

As a follow up, after some experimentation 16 the thing I've found Python most useful 15 for is any situation involving text manipulation 14 (yourStringHere.replace(), regexes for more 13 complex stuff) or testing some basic concept 12 really quickly, which it is excellent for.

For 11 stuff like SQL DB restore scripts I find 10 I still usually just resort to batch files, as 9 it's usually either something short enough 8 that it actually takes more Python code 7 to make the appropriate system calls or 6 I can reuse snippets of code from other 5 people reducing the writing time to just 4 enough to tweak existing code to fit my 3 needs.

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

Score: 1

Python, along with Pywin32, would be fine for Windows 4 automation. However, VBScript or JScript 3 used with the Winows Scripting Host works 2 just as well, and requires nothing additional 1 to install.

Score: 1

I've done a decent amount of scripting in 33 both Linux/Unix and Windows environments, in 32 Python, Perl, batch files, Bash, etc. My 31 advice is that if it's possible, install 30 Cygwin and use Bash (it sounds from your 29 description like installing a scripting 28 language or env isn't a problem?). You'll 27 be more comfortable with that since the 26 transition is minimal.

If that's not an option, then 25 here's my take. Batch files are very kludgy 24 and limited, but make a lot of sense for 23 simple tasks like 'copy some files' or 'restart 22 this service'. Python will be cleaner, easier 21 to maintain, and much more powerful. However, the 20 downside is that either you end up calling 19 external applications from Python with subprocess, popen 18 or similar. Otherwise, you end up writing 17 a bunch more code to do things that are 16 comparatively simple in batch files, like 15 copying a folder full of files. A lot of 14 this depends on what your scripts are doing. Text/string 13 processing is going to be much cleaner in 12 Python, for example.

Lastly, it's probably 11 not an attractive alternative, but you might 10 also consider VBScript as an alternative. I 9 don't enjoy working with it as a language 8 personally, but if portability is any kind 7 of concern then it wins out by virtue of 6 being available out of the box in any copy 5 of Windows. Because of this I've found myself 4 writing scripts that were unwieldy as batch 3 files in VBScript instead, since I can't 2 usually depend on Python or Perl or Bash 1 being available on Windows.

Score: 0

I've been using a lot of Windows Script Files lately. More powerful 2 than batch scripts, and since it uses Windows 1 scripting, there's nothing to install.

More Related questions