0

I'm trying to execute a batch file using a Python script. Is this possible? The Python script is in a different folder than the batch file. For example, the Python script is in C:\users\me\desktop\python while the batch file is in a folder C:\users\me\desktop\batch. I prefer not to use the full path to the batch file because I want it to work on other people's computer as well (i.e. the C:\users\me part might be different).

This is the script I tried (executed from the "python" folder on desktop)

from subprocess import call

path = "..\batch"
call([path+"\test.bat"])

Result: file not found

vdvaxel
  • 667
  • 1
  • 14
  • 41
  • Possible duplicate of (http://stackoverflow.com/questions/5469301/run-a-bat-file-using-python-code) – Chanda Korat Feb 17 '17 at 10:11
  • 2
    Possible duplicate of [Run a .bat file using python code](http://stackoverflow.com/questions/5469301/run-a-bat-file-using-python-code) – Florian Koch Feb 17 '17 at 10:13
  • Possible duplicate of [Executing a subprocess fails](http://stackoverflow.com/questions/1818774/executing-a-subprocess-fails) – feedMe Feb 17 '17 at 10:45

2 Answers2

1

Backslash escapes special characters in python. Therefore, the paths that you are creating here are not the ones you think they are:

In [1]: test = "..\bfoo"

In [2]: test
Out[2]: '..\x08foo'

Use raw strings instead:

In [3]: test = r"..\bfoo"

In [4]: test
Out[4]: '..\\bfoo'

And actually, the best way to combine path segments in python is by using os.path.join. This will automatically take care of the backslash vs. slash issues for Unix-lie vs. Windows operating systems.

languitar
  • 6,554
  • 2
  • 37
  • 62
  • Thanks, that worked. However, when I run the Python script, it now shows me the code inside the batch file without actually executing it? – vdvaxel Feb 17 '17 at 10:18
  • On the command line? Or does it open a new window. However, this is probably actually a different question. – languitar Feb 17 '17 at 10:20
  • Yes it shows the code on the command line and doesn't open a new window. – vdvaxel Feb 17 '17 at 10:24
  • When I put the Python script in the same folder as the batch file, it works. But for some reason it doesn't work from a different folder. – vdvaxel Feb 17 '17 at 10:27
  • Have a look at the marked duplicate question http://stackoverflow.com/questions/1818774/executing-a-subprocess-fails. Maybe it solves the issue? – languitar Feb 17 '17 at 11:05
  • I don't really understand what they're trying to do there, but my code is the same so I don't understand? – vdvaxel Feb 17 '17 at 11:12
  • Maybe executing `cmd.exe /c your.bat` helps? – languitar Feb 17 '17 at 11:14
  • I'm trying now to just include the command in the Python script instead of a separate batch file. Thanks anyway! – vdvaxel Feb 17 '17 at 11:26
  • Would be nice if you could vote or accept the answer, as it resolves the original problem of the code that you have shown. – languitar Feb 17 '17 at 11:27
0

Use os.path,

import os 

dir_path = os.path.dirname(os.path.realpath(__file__))  # get the full path of the Python file
parent_dir = os.path.dirname(dir_path)

new_path = os.path.join(parent_dir, 'bath', 'test.bat')
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134