daniel11
daniel11

Reputation: 2041

comparing batch to python commands?

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!

Upvotes: 1

Views: 3035

Answers (5)

Alain
Alain

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

Katriel
Katriel

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!


  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

Upvotes: 2

Kos
Kos

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

Noufal Ibrahim
Noufal Ibrahim

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

Related Questions