Ryan Anderson
Ryan Anderson

Reputation: 31

Simple batch file issue

So I have a really simple question, and I can't seem to find a really simple answer.

I have a batch file to run my code. This is what's in the batch file:

c:\python27\python.exe filename.py
pause

I want to run this batch file from a different directory than filename.py is in. How do I do this?

Upvotes: 1

Views: 274

Answers (4)

jb.
jb.

Reputation: 10351

You either need to give the full path to the filename as @AbhijeetRastogi said, or you can change your working directory and just use the filename.py as is. To use his example:

pushd C:\Users\username\Desktop\
c:\python27\python.exe filename.py
popd
pause

Per PA's suggestion, I've changed cd to pushd/popd. The difference is that now when the batch file runs, it will go back to where the current working directory was, instead of being in the path where filename.py is

Upvotes: 2

Luminosity
Luminosity

Reputation: 1

From the desktop, right-click My Computer and click Properties.
In the System Properties window, click on the Advanced tab.
In the Advanced section, click the Environment Variables button.

highlight the Path variable in the Systems Variable section and click the Edit button. Add or modify the path lines with the paths you wish the computer to access. Each different directory is separated with a semicolon as shown below.

C:\Program Files;C:\Winnt;C:\Winnt\System32;c:\python27\python.exe

then you should be able to type python.exe filename.py and it will find python and execute the file no matter what directory you are in.

Upvotes: 0

Max
Max

Reputation: 7596

create a .cmd file containing the following command

Start "" /D "c:\Directory_of_your_py_file\" c:\python27\python.exe filename.py

Upvotes: 0

shadyabhi
shadyabhi

Reputation: 17234

c:\python27\python.exe filename.py

You need to give a full path to filename.py such as

c:\python27\python.exe C:\Users\username\Desktop\filename.py

Upvotes: 3

Related Questions