0

I'm working on a personal project as a very amateur programmer and to do this, I need to have python tell cmd to run an external program via command line.

For example, I need to chdir ("C:\blah\blah") on Python, and run externalprogram -w "<destination>\newName.fileType>" "<source>\*.*".

I'm very lost in how to do this and any help would be greatly appreciated.

So far my code looks like this

import os

os.chdir('C:\Program Files (x86)\<externalProgram>')
os.system('<externalCommand> "<destination>\file.fileType" "<source>\*.*"')

Still can't get it to work though no errors are being posted to the shell.

Fishtallen
  • 61
  • 1
  • 2
  • 5
  • 2
    Usually you can use [subprocess.call](http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module) `import subprocess; subprocess.call(['C:\blah\blah\newName.fileType'])`, [How do I execute a program from python?](http://stackoverflow.com/questions/204017/how-do-i-execute-a-program-from-python-os-system-fails-due-to-spaces-in-path) – chickity china chinese chicken Nov 09 '16 at 01:14
  • You might want the effect of `chdir` to last/be upon your python process/script - [os.chdir(path)](https://docs.python.org/2/library/os.html#os.chdir)? – greybeard Nov 09 '16 at 02:05
  • Would you "accept" the preferred answer below? You may accept your own if you wish. To accept, click on the tick mark adjacent to the answer you prefer. Please consider upvoting any others that you thought were helpful; this is not mandatory, but it is nice to do so. Finally I see that Prune's answer is not replied to - please consider making a response to everyone if you can. – halfer Nov 17 '16 at 22:44
  • Downvoted, see above. Happy to undownvote if you can click the tickmark. – halfer Nov 21 '16 at 16:01

4 Answers4

0

The simplest way to do it is:

import os
os.system('your command')
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • I tried this and my cmd flashed open and closed and nothing moved. How could I check what the issue is? – Fishtallen Nov 09 '16 at 10:03
  • @Hallquist If you are trying to execute `os.system('chdir your/dir')`, then it will not have any effect. It opens cmd changes directory in it and closes. If you want to change working directory for current python script then you might want to use `os.chdir('your/dir')` (as @greybeard suggested) – Yevhen Kuzmovych Nov 09 '16 at 10:11
  • I tried and updated my post. I appreciate you taking the time to help! – Fishtallen Nov 09 '16 at 10:22
0

Yes. You need the os.system() method, near the bottom of this page. You pass the command you want to run as a string.

Also, many UNIX system commands are built into this package; you might find the one you want as a specific call.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

Your quotation marks?

os.system('<command> "<destination>\file.fileType" "<source>\*.*") #still in quote

starts with ' ends in "

os.system('<command> "<destination>\file.fileType" "<source>\*.*" ') #closed properly

If you don't want quotations in python to be recognised in quotes put a backslash in front

print (" \" ") # prints out "

say add parameters to the command

destination = "folder\file.fileType"
source = "source\*.*"
os.system('<command> \" ' + destination + ' \" '+ source+' \" ') #closed 
Fishtallen
  • 61
  • 1
  • 2
  • 5
  • So, I've got it to print how I wanted, but am unable to get it to do anything. It just flashes and nothing happens. It seems I can't change the directory properly and it stays on user\pthyon. How could I make it run 2 or more commands in succession in the same cmd window? – Fishtallen Nov 10 '16 at 01:11
  • import os os.chdir('C:\Program Files (x86)') os.system("cd") os.system("PAUSE") – Sebastian H Nov 10 '16 at 03:37
0

So, my main issues were not adding the /D when for my directory change to change the disk as well, and the use of the '&&'.

import os
changeDir = ('cd /D C:\\Program Files (x86)\\externalProgram')
externalCommand = '<Command> \"<destination>\\newName.fileType\" \"<source>\\*.*\"'
os.system(changeDir + ' && ' externalCommand)
halfer
  • 19,824
  • 17
  • 99
  • 186
Fishtallen
  • 61
  • 1
  • 2
  • 5