[Tutor] Running Command Prompt In Python

Alan Gauld alan.gauld at yahoo.co.uk
Thu Feb 29 16:52:06 EST 2024


On 29/02/2024 11:26, Am Ra wrote:

> Importing os, and using os.system to connect to Command Prompt.

os.system is nearly always the wrong approach. os.system is
really only worthwhile if its a simple throwaway command.
For example I use it for an app that manages a list of images
and I have a Show menu option that displays the image in
the systems file viewer. os.system is fine for that.
But for anything where you want to either interact with
the command or to view its output you should use subsystem.
Either Popen or one of its utility functions.

> [image: image.png]

The server strips binary files for security reasons, please
cut 'n paste the text directly into the email.

> Then, dmel922.exe will be activated to generate an empty csv file from a
> raw dataset.

If it's really empty would it not be simpler to just
create it directly from Python? Or is there more going
on in the background?

> Up to here it is working as it is supposed to. However, it goes on a pause
> and not adding any data to the empty csv file... 

Ah, OK that was only half the story! :-)

> ...because it is waiting on the
> user input which is “Enter y to Continue”.  As we see in the picture below,
> the Command Prompt is asking the user to press y to continue. As soon as, I
> press y in the Command Prompt it will continue and finish the task.

Subprocess is your friend in this scenario. Read the
module docs carefully, they are quite detailed and
include several examples.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list