2

I have the following .bat script which is located on the Desktop. I have a python script which resides in a folder ("Folder") also on the Desktop. I can run the .bat script if both files are on the same directory but how I can access the python script when it's in "Folder"? I want this to run on other computers so I am trying to avoid defining usernames (eg. C:\Users\user_name\Desktop) unless there is a generic way to obtain user names and incorporate that into the .bat file.

The following is the .bat script:

@echo off

rem Root OSGEO4W home dir to the following directory
call "C:\OSGeo4W64\bin\o4w_env.bat"

echo.
cmd /k python "Script.py" 
@echo on

I tried inserting cd Folder before cmd /k python "Script.py but that only sets the directory and doesn't follow on to run the python script.

Any advice is appreciated!

3
  • 1
    Do you need cmd at all? I think you can run python directly from the .bat file. That might not solve your main question, but it would be a more natural way to execute a Python script from a .bat file. Commented Feb 26, 2015 at 14:05
  • 1
    @PadraicCunningham - Brilliant, many thanks for that! Although the question is solved, I noted this command down :) Commented Feb 26, 2015 at 14:05
  • @FMc - Thank you for your advice, I will definitely try that as I'm guessing it would work with the answer that's already provided. Commented Feb 26, 2015 at 14:07

2 Answers 2

3

You can just call it as python "folder/script.py".

Although it is slightly odd, because your "cd" solution should have worked as well, so there may be something else going on.

Sign up to request clarification or add additional context in comments.

7 Comments

I believe cmd is starting a subshell, which might not inherit the current working directory from the parent batch script (I don't have a windows machine available to test at the moment)
@Vicky - Ah crap I asked a dumb question! Thank you very much, simple answer to an even simpler question!
@SimonFraser - Thanks for your tip, that makes sense. I will try and test this as I am still on the learning curve :)
@SimonFraser I'm sure you're right. I didn't even notice the "cmd" in there at the front as I am so used to writing python foo.py at the command line or in a batch file.
Just run "folder\script.py", i.e. specifying python should be redundant if your system is configured right . On a related note, if you have Python 3.3+ installed, then the .py extension should be associated with the new py.exe launcher. This lets you add #!python2 and #!python3 shebangs to the first line of your scripts to run using a particular version, without having to modify file type associations.
|
1

use

start python Folder/Script.py

in your bat-file.

It will open a separate command line window and executes the script in that window.

1 Comment

Thanks rherzog, I am aware of the start command but I want it to be executed in the same window (I perhaps should have mentioned it in the question).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.