0

Possible Duplicate:
How to call external command in Python

I want to execute a windows command line operation using python. To execute the command I have to go to a particular directory within my system and then execute the command. for example

1) Go to a particular directory c:\some\directory

2) then use command somecommand -x -y

I saw some posts on this topic but I was not able to figure them out properly.

Thanks

Community
  • 1
  • 1
msd_2
  • 1,117
  • 3
  • 16
  • 22

1 Answers1

1

I assume you want to change the working directory then execute a command. So:

os.chdir(DIRECTORY);
os.system(COMMAND);
  • os.chdir - Set the current working directory.
  • os.system - Execute a "system" command.

If setting the working directory is not required you could just specify the full path to os.system.

Also, you might want to check out subprocess as it might be more what you're looking for.

Community
  • 1
  • 1
Matt Razza
  • 3,524
  • 2
  • 25
  • 29
  • thanks it worked but is there a different method to achieve it, because I have ssen few posts where it is recommended to use subprocess module instead of system – msd_2 Jun 26 '12 at 20:04
  • Correct. You'll likely want to use subprocess as I said. Perhaps this may help: http://stackoverflow.com/questions/3762468/subprocesses-with-different-working-directories-problem-with-python – Matt Razza Jun 26 '12 at 20:06