1

Ok i have these commands used in batch and i wanted to know the commands in python that would have a similar affect, just to be clear i dont want to just use os.system("command here") for each of them. For example in batch if you wanted a list of commands you would type help but in python you would type help() and then modules... I am not trying to use batch in a python script, i just wanna know the similarities in both languages. Like in english you say " Hello" but in french you say "Bonjour" not mix the two languages. (heres the list of commands/functions id like to know:

  1. change the current directory
  2. clear the screen in the console
  3. change the prompt to something other than >>>
  4. how to make a loop function
  5. redirections/pipes
  6. start an exteral program (like notepad or paint) from within a script
  7. how to call or import another python script
  8. how to get help with a specific module without having to type help()

@8: (in batch it would be command /?)

EDITED COMPLETELY

Thanks in Adnvance!

daniel11
  • 2,027
  • 10
  • 38
  • 46
  • You can start notepad with `os.system("start notepad.exe")`, for what it's worth, and run batch files with `os.system("test.bat")`. – Jason Orendorff Dec 11 '10 at 18:41
  • As others have said, you can't go and write batch in python (not only it won't work without using `os.system` everywhere, it'd also be pretty horrible code). Learn the language properly and be enlightened ;) I started out with batch too. It's simple a wholly different (and better, expect perhaps for very simple glue code between existing executables) league. –  Dec 11 '10 at 18:44
  • 2
    Square peg, round hole. One-to-one mapping of batch commands into Python is a horrible way to start learning Python. – Jeet Dec 11 '10 at 18:59
  • 2
    I have trouble even considering batch files a programming language. – Glenn Maynard Dec 11 '10 at 19:21
  • To clear the console screen see [How to clear python interpreter console?](http://stackoverflow.com/questions/517970/how-to-clear-python-interpreter-console). – martineau Dec 11 '10 at 21:28
  • ok sorry for the confusion, ive re-worded my question. just to be clear i dont want to write batch in a python script, i just want to know what commands in python would achieve the same affect as the commands i listed (without using os.system("command here")) – daniel11 Dec 12 '10 at 18:19

5 Answers5

2

You can't just mechanically translate batch script to Python and hope that it works. It's a different language, with different idioms and ways of doing things, not to mention the different purpose.

I've listed some functions related to what you want below, but there's no substitute for just going and learning Python!


  1. os.chdir

  2. os.system("cls") is probably the simplest solution

  3. Change sys.ps1 and sys.ps2.

  4. Nope, there are no gotos in Python. Use for and while loops instead.

  5. Doesn't make sense, use Python's IO instead.

  6. subprocess.Popen

  7. Doesn't make sense, use import or subprocess.Popen instead.

  8. help

Katriel
  • 120,462
  • 19
  • 136
  • 170
  • For 8, if you use the ipython shell, you can add a question mark to any object name to get help (`reduce?`). – Thomas K Dec 11 '10 at 18:40
  • Tempted to downvote this for saying "Doesn't make sense" without elaboration. But these are more or less the right answers. – Jason Orendorff Dec 11 '10 at 18:40
  • @Jason: well, it really doesn't make sense to me... should it flush stdout? Refresh a GUI window? But I've edited it. – Katriel Dec 11 '10 at 18:41
  • 2
    Regarding `cls`: It's similar to `clear` in linux. Stands for "clear screen" methinks, and that's what it does (make console blank). (And why say "doesn't make sense" when you mean "I don't know what this does"?) –  Dec 11 '10 at 18:46
  • @delnan: my point is that Python isn't tied to the shell the way batch script is, so there's no inherent screen to clear! Does it mean that if you run a Python script from a shell it should clear that shell? (In that case, what should it do if you run the same script but not from a shell?) Start a new shell and clear that? `os.system('cls')`? – Katriel Dec 11 '10 at 18:52
  • It's just as possible to redirect a batch file's `stdin` and `stdout` away from a console. And most of the time, there *is* a console in Python. Yes, it's pointless if there is none, but that's also the case for e.g. the `curses` package. –  Dec 11 '10 at 18:57
  • @katrielalex "`cls` in Python" makes sense if you think about it the right way. When `cls` executes in a batch file, that cmd.exe process does... something. What would be the Python code to do that? cmd.exe is written in C or C++; I'm guessing it's doing win32 system calls that a Python program could do. (But `os.system("cls")` gets the same effect with a lot less hassle.) – Jason Orendorff Dec 12 '10 at 15:35
  • @katrielalex when you said "change sys.ps1 and sys.ps2" what did you mean? also, is it possible to use a for or while loop in a xturtle script? (to loop the display of the graphics created by the turtle module) if so an example would really help! – daniel11 Dec 12 '10 at 18:31
  • @daniel: if you do `sys.ps1 = "turtle>"` it will change the prompt to `turtle>`. And I assume `xturtle` is just a Python module? If so you can certainly use a loop: `while True: turtle.step_forwards(1)` or whatever. – Katriel Dec 12 '10 at 21:14
  • ok so is that what i type exactly? cause i dont understand... you said "while true" ... while whats true? – daniel11 Dec 14 '10 at 18:49
  • @daniel: you need to go and learn Python first; I can't teach you all of it in a StackOverflow comments box! http://diveintopython.org/toc/index.html `True` is something that evaluates to, well, true; if you put it in the condition of a loop it will go forever. – Katriel Dec 14 '10 at 22:32
2

Most of the things you've mentioned (start, cls etc.) are not "batch commands", they're executable programs which perform certain tasks. The DOS "shell" simply executes these when it encounters them in a file. In this sense, "python" is the equivalent of a single executable (like cls).

Now that that's clear, cd (and most other OS specific tasks) are accomplished using the os module. There's no single Python statement to clear the screen - that would be wasteful. Changing the prompt of the python interpreter can be done by assigning to sys.ps1. Loops are done using while or for. Redirection doesn't happen. YOu can however use the subprocess module to run subcommands and send their outputs to files or other streams. Starting commands is done using the subprocess.Popen function. For getting help, you can either do help("command") or if you're using ipython, just say command? and hit enter.

You should really go through the tutorial rather than trying to map batch commands to Python.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 1
    Erm, no. Most of the things he mentioned (and all of those *you* bring as an example) are built-in commands of the `cmd` shell. Not individual programs. You can try locating them with `where` (an actual program) ... – Joey Dec 12 '10 at 00:35
  • Ah. I stand corrected. Thanks. My point about trying to translate batch commands to Python still stands though. – Noufal Ibrahim Dec 12 '10 at 07:57
  • "translating batch commands to python..." is a bad way of describing my intentions.. i am just to to achieve the same affect in python that these commands do in batch.. like, if in batch cls clears the screen , i wanna know how to do that in python or changing the current directory .. and so on ... (that might have been a bit hipocritical but hopefully you get my point) – daniel11 Dec 14 '10 at 18:52
  • You *are* (atleast partially) trying to translate shell into Python. Redirection/pipes is a good example. katrielalex's answer is the best one so far and tells you the answer to your specific questions. You notice the gaps in what he said though. That's because Python is designed to do something other than stringing shell commands together. – Noufal Ibrahim Dec 14 '10 at 19:36
1

The Python docs are excellent, and are the place to start. For doing shell-script like things, you'll want to check out:

Adam Vandenberg
  • 19,991
  • 9
  • 54
  • 56
1

Python is not a system shell, Python is a multi-paradigm programming language.

If you want to compare .bat with anything, compare it with sh or bash. (You can have those on various platforms too - for example, sh for windows is in the MinGW package).

Kos
  • 70,399
  • 25
  • 169
  • 233
0

I am pretty much facing the same problem as you, daniel11. As a solution, I am learning BATCH commands and their meaning. After I understand those, I am going to write a program in Python that does the same or accomplishes the same task.

Thanks to Adam V. and katrielatex for their insight and suggestions.

radu florescu
  • 4,315
  • 10
  • 60
  • 92
Alain
  • 157
  • 1
  • 3
  • 14