I need to run several commands more than 400 times for a mass number of files. Therefore, I'm trying to incorporate the commands in to a python script to automate the processes. Even though the commands run successfully in the the windows command prompt (I have manually tested the commands), they do not run successfully in python using os.system module as below.

import os
os.system('command')

For some commands such as 'cd' or 'dir', I get 0 for the exit code. For other commands I am getting 1 or 2 for the exit code (sometimes I get both). As I understand it 0 indicates success and 1 or 2 means the process failed.

Any suggestions? Thanks in advance....

Recommended Answers

All 3 Replies

pyTony, thank you for the reply.

I agree, believe me I wouldn't even think of using commands except that what I am trying to do only works from command line. I used the command examples 'cd' and 'dir' only as examples, I know that these can be done very simply in python without using commands.

If it helps, I am trying to run TauDEM tools (open source from USU) which are used for extracting/analyzing Digital Elevation Models (DEMs). These DEMs are in tiff file format. The tools work in conjuction with other software including MPICH2 and ArcGIS 10.0. The most recent (and currently supported) release is only developed for command line.

I have read about running subprocesses from python (your link is helpful) but I'm new to this (it's complicated!) so I'm still trying to grasp it all.

Thanks again for suggestions...

Use this snippet, it works well http://www.daniweb.com/software-development/python/code/257449 .
Example (with a linux command)

>>> com = Command("ls -l").run()
>>> if com.failed:
...  print "ERROR"
...  print com.error
... else:
...  print com.output
... 
total 20
drwxr-xr-x  2 eric eric 4096 2011-09-02 14:35 cons
drwxr-xr-x 10 eric eric 4096 2011-09-08 16:02 img
drwxr-xr-x  4 eric eric 4096 2011-09-05 19:04 notes
drwxr-xr-x  2 eric eric 4096 2011-09-08 20:29 src
drwxr-xr-x  2 eric eric 4096 2011-09-08 20:30 tmp
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.