Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have downloaded the Python package from the website. It is in my C drive, in a directory named Python27. In the command prompt, I type the following:

cd C:\Python27
python

Then I get Python 2.7.4. and the opening message. My Python file, called first.py, is in the desktop, and I don't know how to access it. I've tried the following..

python first.py
python first.py
python cd Desktop\first.py
python cd Desktop\"first.py"

What is the correct command?

share|improve this question

migrated from stackoverflow.com Oct 7 '13 at 7:58

This question came from our site for professional and enthusiast programmers.

    
Are you trying to change directory to the folder holding the file? (Also, if you could put the output when you attempted those commands, it would help.) –  BlackVegetable Oct 6 '13 at 21:49
    
The Desktop directory on Windows is C:\Users\<USERNAME_HERE>\Desktop, I believe. –  li.davidm Oct 6 '13 at 21:49
    
Did you try C:\Python27\python.exe C:\Users\username\desktop\first.py ? (assuming no space in the username and no c:\python in the path). –  Hennes Oct 28 at 21:47
    
Alternatively, right click on the python file on the desktop and select [always] open with this program. Then select the python binary. –  Hennes Oct 28 at 21:49

5 Answers 5

You have to add python to your path, then you can open a command line in the directory of your file and type:

python first.py
share|improve this answer
    
i have already added python to my path, i think –  Yuen Hsi Oct 6 '13 at 22:32
    
Then open up a command line, cd to your file's directory, and type the command I wrote above. –  Jakob Weisblat Oct 6 '13 at 22:42
    
Note that you need to type this BEFORE opening python –  Jakob Weisblat Oct 6 '13 at 22:45

If your program does not need the console (e.g. automation scripts or GUI programs), then double click on the script file to execute it.

Otherwise, in modern Windows (Vista, Seven, 8), you folder is located under \Users. Let's say the script script.py is the Desktop folder of user Foo, so it would be:

python c:\Users\Foo\Desktop\script.py
share|improve this answer
    
i did that; i got a syntax error: invalid syntax. command prompt thinks i'm typing code instead of trying to run a file? :( –  Yuen Hsi Oct 6 '13 at 22:35
    
You'd have to type it in outside of python –  Jakob Weisblat Oct 6 '13 at 22:43

Go like this:

  • open a console
  • > cd Desktop
  • > c:\Python27\python first.py

Best to add C:\Python27 to the system PATH variable though.

share|improve this answer
    
i get can't open file 'first.py' ; no such file or directory :( –  Yuen Hsi Oct 6 '13 at 22:33

You can also do this in command prompt:

start C:\Users\your_username_here\Desktop\first.py

You can even do this in python! Here is how:

import os
os.system("start "+yourPathHere)

A very simple program would be:

import os
while True:
    userInput=str(input("Enter file/app path: "))
    os.system("start "+userInput)
    print ("") # - just for the looks -

The more you know :p

share|improve this answer

Make sure you set up the path like this by adding this in the Environmental Variables: %path% on your settings. C:\WINDOWS\system32;C:\WINDOWS;C:\Python27

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.