954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

os.system(command) in python

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....

momartin
Newbie Poster
2 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Use Python commands, like os.listdir(os.curdir), not system commands. Better you make code in Python for whole thing which is goal of your commands. Also you should use subprocess if you do need to run a command: http://jimmyg.org/blog/2009/working-with-python-subprocess.html

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

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...

momartin
Newbie Poster
2 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: