python as a "batch" script (i.e. run commands from python)

I'm working in a windows environment (my laptop!) and I need a couple of scripts that run other programs, pretty much like a windows batch file.

how can I run a command from python such that the program when run, will replace the script? The program is interactive (for instance, unison) and keeps printing lines and asking for user input all the time.

So, just running a program and printing the output won't suffice. The program has to takeover the script's input/output, pretty mcuh like running the command from a .bat file.

I tried os.execl but it keeps telling me "invalid arguments", also, it doesn't find the program name (doesn't search the PATH variable); I have to give it the full path ..?!

basically, in a batch script I can write: unison profile

how can I achieve the same effect in python?


I found out it can be done with os.system( ... ) and since I cannot accept my own answer, I'm closing the question.

EDIT: this was supposed to be a comment, but when I posted it I didn't have much points.

Thanks Claudiu, that's pretty much what I want, except for a little thing: I want the function to end when the program exits, but when I try it on unison, it doesn't return control to the python script, but to the windows command line environment

>>> os.execlp("unison")

C:\>Usage: unison [options]
    or unison root1 root2 [options]
    or unison profilename [options]

For a list of options, type "unison -help".
For a tutorial on basic usage, type "unison -doc tutorial".
For other documentation, type "unison -doc topics".


how to get around this?

Asked by: Oliver548 | Posted: 06-10-2021

Answer 1

You should create a new processess using the subprocess module.

I'm not fluent in windows processes but its Popen function is cross-platform, and should be preffered to OS specific solutions.

EDIT: I maintain that you should prefer the Subprocess module to os.* OS specific functions, it is cross-platform and more pythonic (just google it). You can wait for the result easily, and cleanly:

import os
import subprocess
unison = os.path.join(os.path.curdir, "unison")
p = subprocess.Popen(unison)

Answered by: Chester600 | Posted: 07-11-2021

Answer 2

I found out that os.system does what I want,

Thanks for all that tried to help.


runs the command just as if it was run from a batch file

Answered by: Kelvin172 | Posted: 07-11-2021

Answer 3

import subprocess

proc = subprocess.Popen(['unison', 'profile'], stderr=subprocess.PIPE,      
                        stdout=subprocess.PIPE, stdin=subprocess.PIPE)

proc.stdin.write('user input')

This should help you get started. Please edit your question with more information if you want a more detailed answer!

Answered by: Miranda811 | Posted: 07-11-2021

Answer 4

os.execlp should work. This will search your path for the command. Don't give it any args if they're not necessary:

>>> import os
>>> os.execlp("cmd")

D:\Documents and Settings\Claudiu>Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\Documents and Settings\Claudiu>

Answered by: Daryl666 | Posted: 07-11-2021

Similar questions

scripting - Python script to run Django commands

I want to run a python scripts which should do: Create a django project: django-admin startproject foobar Create a app in the project: python barfoo Add an entry of newly created app barfoo in the setting's INSTALLED_APP. How can I achieve this?

Scripting git commands in Python without external library

I am trying to script a git pull without having to enter a username and password. How would I go about doing this? The only functionality I will be using from git is "git pull".

Python Scripting : how to execute unix commands inside gdb prompt using python script..?

I want to execute linux commands (eg. bt, break, frame etc) inside gdb prompt using python scripting. for example: i am using["gdb"], shell=True) this line takes me to (gdb) prompt by executing gdb command but after it when i try["backtrace"], shell=True) it shows /bin/sh:backtrace: command not found

scripting - How do I check out a file from perforce in python?

I would like to write some scripts in python that do some automated changes to source code. If the script determines it needs to change the file I would like to first check it out of perforce. I don't care about checking in because I will always want to build and test first.

scripting - What would be the simplest way to daemonize a python script in Linux?

What would be the simplest way to daemonize a python script in Linux ? I need that this works with every flavor of Linux, so it should only use python based tools.

scripting - Make python enter password when running a csh script

I'm writing a python script that executes a csh script in Solaris 10. The csh script prompts the user for the root password (which I know) but I'm not sure how to make the python script answer the prompt with the password. Is this possible? Here is what I'm using to execute the csh script: import commands commands.getoutput('server stop')

scripting - Python code for sorting files into folders

Python 2.5.1 I've just run PhotoRec and the code given as a way to sort file types into their own folder is coming back with this error. Any suggestions on how to alter? Thanks : [EDIT2: Two points: This question was voted down because it was a...

python - How can I call a DLL from a scripting language?

I have a third-party product, a terminal emulator, which provides a DLL that can be linked to a C program to basically automate the driving of this product (send keystrokes, detect what's on the screen and so forth). I want to drive it from a scripting language (I'm comfortable with Python and slightly less so with Perl) so that we don't have to compile and send out executables to our customers whenever there's a p...

scripting - Change directory to the directory of a Python script

How do I change directory to the directory with my Python script in? So far, I figured out I should use os.chdir and sys.argv[0]. I'm sure there is a better way then to write my own function to parse argv[0].

php - Get remote text file, process, and update database - approach and scripting language to use?

I've been having to do some basic feed processing. So, get a file via ftp, process it (i.e. get the fields I care about), and then update the local database. And similarly the other direction: get data from db, create file, and upload by ftp. The scripts will be called by cron. I think the idea would be for each type of feed, define the ftp connection/file information. Then there should be a translation of how data...

scripting - cProfile and Python: Finding the specific line number that code spends most time on

I'm using cProfile, pstats and Gprof2dot to profile a rather long python script. The results tell me that the most time is spent calling a method in an object I've defined. However, what I would really like is to know exactly what line number within that function is eating up the time. Any idea's how to get this additional information? (By the way, I'm using Python 2.6 on OSX snow leopard if that h...

php - How do scripting languages use sockets?

Python, Perl and PHP, all support TCP stream sockets. But exactly how do I use sockets in a script file that is run by a webserver (eg Apache), assuming I only have FTP access and not root access to the machine? When a client connects to a specific port, how does the script file get invoked? Does the script s...

html - Python for web scripting

I'm just starting out with Python and have practiced so far in the IDLE interface. Now I'd like to configure Python with MAMP so I can start creating really basic webapps — using Python inside HTML, or well, vice-versa. (I'm assuming HTML is allowed in Python, just like PHP? If not, are there any modules/template engines for that?) What modules do I need to install to run .py from my localhost? Googling a bit, it s...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python