1

I am rather new to Python and have been trying to run a .cmd file with it, but it won't run it from the correct location. My file Run_setup.cmd, is setting up another a different software with a bunch of related files and so I have sequestered them to their own folder for my sanity.

Currently I can get the .cmd file to run from the same location as my source code. I know I am messing up the file path for it with cwd=r'%s' based on what the documentation says, but I don't get how.

If cwd is not None, the function changes the working directory to cwd before executing the child. cwd can be a str and path-like object. In particular, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path.

I currently have it using cwd=r' C:\LargeFolder\Files\CorrectFolder' based off this post, and it seems that it works for any file path, but I can't seem to get it to work for me.

from subprocess import Popen

def runCmdfile():
    # File Path to source code:    'C:\LargeFolder\Files'
    myDir = os.getcwd()

    # File Path to .cmd file:      'C:\LargeFolder\Files\CorrectFolder'
    myDir = myDir + '\CorrectFolder'

    runThis = Popen('Run_setup.cmd', cwd=r'%s' % myDir)

    stdout, stderr = runThis.communicate()

What am I missing here, and furthermore what is the purpose of using cwd=r' ' ?

3 Answers3

0

the parameter is cwd=. The r"" part only needs to exist in the definition of your string, to have a raw string and make python ignore special sequences using backslashes.

Since your string comes from os.getcwd, you don't need it.

def runCmdfile():
    # File Path to source code:    'C:\LargeFolder\Files'
    myDir = os.getcwd()

    # File Path to .cmd file:      'C:\LargeFolder\Files\CorrectFolder'
    myDir = os.path.join(myDir, 'CorrectFolder')

    runThis = Popen('Run_setup.cmd', cwd=myDir)

    stdout, stderr = runThis.communicate()
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • When using your code I keep getting the error 'FileNotFoundError: [WinError 2] The system cannot find the file specified'. I don't know why it can't find the file as I have confirmed it is in the right folder. – NeverSundae Jul 05 '18 at 19:51
  • @NeverSundae does `print(myDir)` right before `runThis ...` really show the correct path? you may try even a `print('*'+myDir+'*')` to detect leading/trailing blanks etc. – AcK Jul 05 '18 at 20:49
  • @ack I tried printing like you suggested, but there are no blanks. – NeverSundae Jul 05 '18 at 21:01
  • @nosklo I seemed to have solved my problem, does anyone know why this seems to work?: runThis = Popen('C:\LargeFolder\Files\CorrectFolder\Run_setup.cmd') – NeverSundae Jul 05 '18 at 21:14
0

Your error is due to not escaping your \. You need to escape your "\" where you're adding in your subfolder and then you should be good to go.

myDir = myDir + '\CorrectFolder'

should be

myDir = myDir + '\\CorrectFolder'
Mike from PSG
  • 5,312
  • 21
  • 39
0

this one works for me:

def runCmdfile():
    # File Path to source code:    'C:\LargeFolder\Files'
    myDir = os.getcwd()

    # File Path to .cmd file:      'C:\LargeFolder\Files\CorrectFolder'
    myDir = os.path.join(myDir, 'CorrectFolder')

    # Popen does not take cwd into account for the file to execute
    # so we build the FULL PATH on our own
    runThis = Popen(os.path.join(myDir, 'Run_setup.cmd'), cwd=myDir)

    stdout, stderr = runThis.communicate()
AcK
  • 2,063
  • 2
  • 20
  • 27