Reputation: 2041
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:
@8: (in batch it would be command /?)
EDITED COMPLETELY
Thanks in Adnvance!
Upvotes: 1
Views: 3035
Reputation: 177
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.
Upvotes: 0
Reputation: 123772
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!
os.chdir
os.system("cls")
is probably the simplest solution
Change sys.ps1
and sys.ps2
.
Nope, there are no gotos in Python. Use for
and while
loops instead.
Doesn't make sense, use Python's IO instead.
Doesn't make sense, use import
or subprocess.Popen
instead.
help
Upvotes: 2
Reputation: 72319
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).
Upvotes: 1
Reputation: 72855
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.
Upvotes: 2
Reputation: 20671
The Python docs are excellent, and are the place to start. For doing shell-script like things, you'll want to check out:
Upvotes: 1