Would Python make a good substitute for the Window

2019-03-08 00:39发布

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.

Is Python suitable for such things? Moving files around, creating scripts to do things like unzipping a backup and restoring a SQL database, etc.

12条回答
虎瘦雄心在
2楼-- · 2019-03-08 01:15

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:

查看更多
We Are One
3楼-- · 2019-03-08 01:18

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

查看更多
乱世女痞
4楼-- · 2019-03-08 01:21

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/

查看更多
迷人小祖宗
5楼-- · 2019-03-08 01:22

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.

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.

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.

查看更多
不美不萌又怎样
6楼-- · 2019-03-08 01:24

Are you aware of PowerShell?

查看更多
Viruses.
7楼-- · 2019-03-08 01:25

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.

查看更多
登录 后发表回答